사진이 포함 된 중첩 된 매개 변수가 포함 된 양식을 사용하는 비교적 간단한 Rails 앱이 있습니다. 그것은 다음과 같습니다RestKit 및 RKParams의 문제
나는 시험 새로운 형태, 그냥 모든 작품, 그것은 않도록 좋은 예쁜하게 설정class List < ActiveRecord::Base
has_many :list_items
has_many :people, through: list_items
end
class ListItem < ActiveRecord::Base
belongs_to :list
belongs_to :person, autosave: true
has_attached_file :picture,
:storage => :s3,
:bucket => 'myfreebielist',
:s3_credentials => {
:access_key_id => ENV['S3_KEY'],
:secret_access_key => ENV['S3_SECRET']
}
def autosave_associated_records_for_person
if new_person = Person.find_by_name(Person.name) then
self.person = new_person
else
self.person.save!
end
end
end
class Person < ActiveRecord::Base
has_many :list_items
end
:
<%= form_for @list do |f| %>
List name: <%= f.text_field :name %><br/>
<%= f.fields_for :list_items do |lif| %>
<%= lif.fields_for :person do |pif| %>
Person name: <%= pif.text_field :name %>
<% end %>
Picture: <%= lif.file_field :picture %>
<br/>
<% end %>
<%= f.submit %>
<% end %>
OK, 그래서 할 수 있도록하려면를 목록 항목 (및 관련 그림 이미지) 및 해당 목록 항목의 사람과 함께 새 목록을 게시 할 수 있습니다. 나는이 코드를 Objective C에서 테스트 중이다.
NSMutableDictionary *listParams = [NSMutableDictionary dictionary];
[listParams setValue:@"Test One" forKey:@"name"];
NSMutableDictionary *listItems = [NSMutableDictionary dictionary];
// Generate each list item -- a person and a photo.
NSDictionary *person1 = [NSDictionary dictionaryWithKeysAndObjects:@"name", @"John Smith", nil];
NSDictionary *listItem1 = [NSDictionary dictionaryWithKeysAndObjects:@"person_attributes", person1, nil];
[listItems setValue:listItem1 forKey:@"0"];
[listParams setValue:listItems forKey:@"list_items_attributes"];
NSMutableDictionary *params = [NSMutableDictionary dictionary];
[params setValue:listParams forKey:@"list"];
[[RKClient sharedClient] post:@"/lists" params:params delegate:self];
이것은 그림의 주목할만한 부분과 함께 잘 동작한다.
지금, 당신이 요청의 유형을 처리하는 거 야 방법 대신 NSDictionary
의 RKParams
를 사용하는 것은 이해하지만 내가 RKParams
를 사용하여 게시 할 때 레일 측에 도착 이상한 일들의 모든 방식이있다 - 키 주위에 줄 바꿈 문자, 공백 문자 등이 많이 있습니다. 나는 person1
의 일부로 첨부 파일과 함께 RKParams
을 사용해 보았습니다.하지만 객체 RKParams 객체의 이름에 대한 문자열 표현을 보내는 것 같습니다.
나는 너무 가깝다고 느낀다. 그러나 나는 지금까지 몇 시간 동안 내 머리를 때렸다. 어떤 도움을 주시면 감사하겠습니다.
환경 : Xcode 4.3, Rails 3.2.1.
안녕하세요, JSON을 지원하도록 서버를 조정하면 무엇을 의미합니까? 나는 RK에서 같은 문제에 직면하고있다. 나는이 질문을 우연히 만난다. 감사! –
사실, 그것은 내 문제를 해결하지 못했습니다. 나는 AFNetworking으로 가서 문제를 해결하려고 노력했고, 나는 새롭고 창의적인 문제를 겪고있다. 내 컨트롤러에 'respond_to : json'을 추가해야한다는 의미였습니다. –
내 비슷한 다른 문제에 대한 정보가 더 있습니다. 중첩 된 NSDictionary가 \ n과 공백으로 인해 문제를 일으켰습니다. 중첩 된 키의 값으로 NSDictionary 대신 텍스트에 수동으로 중첩 된 키를 사용하여 단일 NSDictionary를 보냄으로써. –