레일 4를 사용 중입니다.중첩 된 양식 값을 DB에 삽입하는 방법
내 프로젝트에는 중첩 된 양식 has_many
관계를 포함하십시오. UI의 관점에서, 나는 그것을 얻었다. 그러나 중첩 된 양식 값은 데이터베이스에 삽입되지 않습니다. 같은
class Newspaper < ActiveRecord::Base
has_to :newspaper_categories, :dependent_destroy => true
accepts_nested_attributes_for :newspaper_categories, :allow_destroy => true, :reject_if => :all_blank
end
class NewspaperCategory < ActiveRecord::Base
belongs_to :newspaper
end
신문 양식의 내용, 내 신문 컨트롤러에서
<%= nested_form_for(@newspaper) do |f| %>
# Newspaper form fields
# Include `Newspaper category` form from the file.
<%= f.fields_for :newspaper_categories do |nc|%>
<%= render "newspaper_category" %>
<% end %>
# For add new form using JS
<%= f.link_to_add "Add New", :newspaper_categories %>
<%= f.submit %>
<% end %>
, 내가 잘못
# add build in new method,
def new
@newspaper = Newspaper.new
@newspaper.newspaper_categoried.build
end
# In params set task_attributes,
def newspaper_params
params.require(:newspaper).permit(:name, :logo, task_attributes[:cat_link, :_destroy])
end
여전히 내가
이
[Rails 4 - 강력한 매개 변수 - 중첩 된 객체] 가능한 복제본 (http://stackoverflow.com/questions/18436741/rails-4-strong-parameters-nested-objects) – emaillenin
'@ newspaper.newspaper_categoried.build' 오타예요, 맞습니까? –