Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. Here 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
like image 810
Rails beginner Avatar asked Jun 02 '11 08:06

Rails beginner


1 Answers

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| %>
like image 188
Hck Avatar answered Oct 13 '22 19:10

Hck