class Contact < MailForm::Base
attribute :phone
attribute :email, :validate => /\A([\w\.%\+\-]+)@([\w\-]+\.)+([\w]{2,})\z/i,
presence: false, allow_blank: true
validate :at_least_a_contact
def headers
{
:subject => "My Contact Form",
:to => "admin@admin.com",
:from => %("#{phone}" <#{email}>)
}
end
private
def at_least_a_contact
unless phone.present? || email.present?
errors.add(:contact, "You need at least a contact method")
end
end
end
def item_params
params.require(:item).permit(:name, :description, :cost,
category_attributes: [:id, :name])
end
def create
@item = Item.new(item_params)
@category = @item.create_category(params[:category])
def new
@item = Item.new
@category = @item.build_category
end
@implementation PhotosCollectionViewController
static NSString * const reuseIdentifier = @"Cell";
- (void)viewDidLoad {
[super viewDidLoad];
[self.collectionView registerClass:[PhotoCollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
self.accessToken = [userDefaults objectForKey:@"accessToken"];
if (self.accessToken == nil) {
SimpleAuth.configuration[@"instagram"] = @{
@"client_id" : @"XXX",
SimpleAuthRedirectURIKey : @"XXX"
};
[SimpleAuth authorize:@"instagram" completion:^(NSDictionary *responseObject, NSError *error) {
NSString *accessToken = responseObject[@"credentials"][@"token"];
[userDefaults setObject:accessToken forKey:@"accessToken"];
[userDefaults synchronize];
}];
} else{
NSString *string = [[NSString alloc] initWithFormat:@"https://api.instagram.com/v1/users/{user-id}/media/recent?access_token=%@",self.accessToken];
NSURL *url = [NSURL URLWithString:string];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
operation.responseSerializer = [AFJSONResponseSerializer serializer];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
//self.photos = [responseObject objectForKey:@"data"];
self.photos = [[[[responseObject valueForKey:@"data"]valueForKey:@"images"]valueForKey:@"low_resolution"]valueForKey:@"url"];
[self.collectionView reloadData];
//NSLog(@"%@",responseObject);
NSLog(@"%@", self.photos);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Request Failed: %@, %@", error, error.userInfo); }
];
[operation start];
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
#pragma mark <UICollectionViewDataSource>
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return self.photos.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
PhotoCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
// Configure the cell
NSString *url = [self.photos objectAtIndex:indexPath.row];
cell.image.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:url]]];
return cell;
}