2

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

我的 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

我的 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)
4

3 回答 3

7

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


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

因此,只需将其用于链接模型:


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

并在d.input :link必要时更改(即,如果 Link 表实际上没有link字段。)

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

于 2011-04-27T00:09:02.940 回答
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| %>
于 2011-04-27T01:29:24.213 回答
0

在您的 konkurrancer 模型中使用

attr_accessible :link_attribute

accepts_nested_attributes_for :link
于 2013-10-28T15:31:12.640 回答