Rails表单帮助has_one belongs_to association

时间:2011-04-26 23:46:22

标签: ruby-on-rails ruby ruby-on-rails-3 simple-form

我正在尝试为链接创建一个简单的嵌套输入字段。

我的konkurrancer模型:

accepts_nested_attributes_for :link
has_one :link

我的链接模型:

class Link < ActiveRecord::Base
belongs_to :konkurrancer
end

我的konkurrencer形式:

    <%= simple_form_for [@konkurrancer] do |f| %>
    <%= f.simple_fields_for :link do |d| %>
    <%= d.input :link %>
    <% end %>
    <%= f.button :submit, :value => 'Opret konkurrence' %>
    <% end %>

当我提交表单时,我收到此错误:

ActiveRecord::UnknownAttributeError in Admin/konkurrancersController#create

unknown attribute: links

参数:

{"utf8"=>"✓",
 "authenticity_token"=>"MACFM37hX4S6XA9vryn7gtfl21PvcaPBSiKDI8mfurg=",
 "konkurrancer"=>{"name"=>"sadasd",
 "links"=>{"link"=>"http://localhost:3000/konkurrancers/new"},
 "commit"=>"Opret konkurrence"}

我的控制器:

  def create
    @konkurrancer = Konkurrancer.new(params[:konkurrancer])

    respond_to do |format|
      if @konkurrancer.save
        format.html { redirect_to(:admin_konkurrancers, :notice => 'Konkurrancer was successfully created.') }
        format.xml  { render :xml => :admin_konkurrancers, :status => :created, :location => @konkurrancer }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @konkurrancer.errors, :status => :unprocessable_entity }
      end
    end
  end

My Rails控制台日志:

Started POST "/admin/konkurrancers" for 127.0.0.1 at 2011-04-27 01:40:50 +0200
  Processing by Admin::KonkurrancersController#create as HTML
  Parameters: {"utf8"=>"Ô£ô", "authenticity_token"=>"MACFM37hX4S6XA9vryn7gtfl21P
vcaPBSiKDI8mfurg=", "konkurrancer"=>{"name"=>"sadasd", "banner2"=>"asdasdas", "t
racking"=>"asdasdd", "vaerdi"=>"3434", "tid"=>"2 min", "tag_tokens"=>"2", "links
"=>{"link"=>"http://localhost:3000/admin/konkurrancers/new"}, "kategori_id"=>"10
", "form"=>"Nyhedsbrev", "partner"=>"Orville", "udtraekkes(1i)"=>"2011", "udtrae
kkes(3i)"=>"27", "udtraekkes(2i)"=>"4", "note"=>""}, "commit"=>"Opret konkurrenc
e"}
  ←[1m←[36mKonkurrancer Load (1.0ms)←[0m  ←[1mSELECT `konkurrancers`.* FROM `kon
kurrancers`←[0m
  ←[1m←[35mCACHE (0.0ms)←[0m  SELECT `konkurrancers`.* FROM `konkurrancers`
  ←[1m←[36mCACHE (0.0ms)←[0m  ←[1mSELECT `konkurrancers`.* FROM `konkurrancers`←
[0m
  ←[1m←[35mAdmin Load (1.0ms)←[0m  SELECT `admins`.* FROM `admins` WHERE (`admin
s`.`id` = 1) LIMIT 1
  ←[1m←[36mTag Load (1.0ms)←[0m  ←[1mSELECT `tags`.* FROM `tags` WHERE (`tags`.`
id` = 2) LIMIT 1←[0m
  ←[1m←[35mSQL (1.0ms)←[0m  BEGIN
  ←[1m←[36mSQL (0.0ms)←[0m  ←[1mCOMMIT←[0m
Completed   in 314ms

ActiveRecord::UnknownAttributeError (unknown attribute: links):
  app/controllers/admin/konkurrancers_controller.rb:48:in `new'
  app/controllers/admin/konkurrancers_controller.rb:48:in `create'

Rendered C:/Ruby192/lib/ruby/gems/1.9.1/gems/actionpack-3.0.3/lib/action_dispatc
h/middleware/templates/rescues/_trace.erb (2.0ms)
  ←[1m←[35mKonkurrancer Load (0.0ms)←[0m  SELECT `konkurrancers`.* FROM `konkurr
ancers` LIMIT 15 OFFSET 0
  ←[1m←[36mSQL (4.0ms)←[0m  ←[1mSHOW TABLES←[0m
  ←[1m←[35mSQL (4.0ms)←[0m  SHOW TABLES
  ←[1m←[36mSQL (4.0ms)←[0m  ←[1mSHOW TABLES←[0m
Rendered C:/Ruby192/lib/ruby/gems/1.9.1/gems/actionpack-3.0.3/lib/action_dispatc
h/middleware/templates/rescues/_request_and_response.erb (266.0ms)
Rendered C:/Ruby192/lib/ruby/gems/1.9.1/gems/actionpack-3.0.3/lib/action_dispatc
h/middleware/templates/rescues/diagnostics.erb within rescues/layout (324.0ms)

3 个答案:

答案 0 :(得分:7)

您希望传递给控制器​​的参数看起来像这样:


            {"konkurrancer"=>{
                   "name"=>"sadasd",
                   "link_attributes"=>{"link"=>"http://localhost:3000/konkurrancers/new"},
                   "commit"=>"Opret konkurrence"
                     }
              }
假设您的链接模型确实有link字段。

所以只需将其用于链接模型:


  <%= f.simple_fields_for :link_attributes do |d| %>

如有必要,可以更改d.input :link(例如,如果链接表没有真正的link字段。)

http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html

答案 1 :(得分:2)

我将在控制器的新操作中构建对象

  

@konkurrancer = Konkurrancer.new

     

@link = @ konkurrancer.build_link

然后以

的形式使用它
<%= f.simple_fields_for :link, @link do |d| %>

或在视图中创建一个新链接:

<%= f.simple_fields_for :link, Link.new do |d| %>

答案 2 :(得分:0)

在你的konkurrancer模型中使用

attr_accessible :link_attribute

accepts_nested_attributes_for :link