Rails edit form not showing nested item
I got a form that have a nested link. The problem that the link field is empty on edit. H开发者_JS百科ere is my form:
<h1>Editing kategori</h1>
<%= simple_form_for(@konkurrancer, :url => {:action => 'update', :id => @konkurrancer.id }) do |f| %>
<%= f.simple_fields_for :link_attributes do |d| %>
<%= d.input :link, :label => 'Tracking url', :style => 'width:500;' %>
<% end %>
<%= f.button :submit, :value => 'Edit konkurrence' %>
<% end %>
<%= link_to 'Show', admin_konkurrancer_path %> |
<%= link_to 'Back', admin_konkurrancer_path %>
My konkurrencer model:
has_one :link
My link model:
class Link < ActiveRecord::Base
belongs_to :konkurrancer
accepts_nested_attributes_for :konkurrancer
end
My konkurrancer edit action:
def edit
@konkurrancer = Konkurrancer.find(params[:id])
@konkurrancer.link_attributes.build
end
1) Remove from your Link model
accepts_nested_attributes_for :konkurrancer
and add to your Konkurrancer model
accepts_nested_attributes_for :link
2) In controller edit action remove
@konkurrancer.link_attributes.build
and in controller new action add
@konkurrances.build_link
3) In the view file replace
<%= f.simple_fields_for :link_attributes do |d| %>
with
<%= f.simple_fields_for :link do |d| %>
精彩评论