Rails 表单帮助 has_one owns_to 关联

发布于 2024-11-03 15:54:07 字数 3729 浏览 0 评论 0原文

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

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

I am trying to create an simple nested input field for links.

My konkurrancer model:

accepts_nested_attributes_for :link
has_one :link

My link model:

class Link < ActiveRecord::Base
belongs_to :konkurrancer
end

My konkurrencer form:

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

When I submit my form I get this error:

ActiveRecord::UnknownAttributeError in Admin/konkurrancersController#create

unknown attribute: links

Parameters:

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

My controller :

  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 console log:

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

定格我的天空 2024-11-10 15:54:08

在您的 konkurrancer 模型中使用

attr_accessible :link_attribute

accepts_nested_attributes_for :link

in your konkurrancer model use

attr_accessible :link_attribute

accepts_nested_attributes_for :link
七月上 2024-11-10 15:54:07

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


            {"konkurrancer"=>{
                   "name"=>"sadasd",
                   "link_attributes"=>{"link"=>"http://localhost:3000/konkurrancers/new"},
                   "commit"=>"Opret konkurrence"
                     }
              }

that's assuming that your Link model really has a link field.

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


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

如有必要,请更改 d.input :link(即,如果链接表实际上没有 link 字段。)

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

You want the params passed to the controller to look something like this:


            {"konkurrancer"=>{
                   "name"=>"sadasd",
                   "link_attributes"=>{"link"=>"http://localhost:3000/konkurrancers/new"},
                   "commit"=>"Opret konkurrence"
                     }
              }

that's assuming that your Link model really has a link field.

So just use this for the link model:


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

And change the d.input :link if necessary (i.e., if the Link table doesn't really have a link field.)

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

输什么也不输骨气 2024-11-10 15:54:07

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

@konkurrancer = Konkurrancer.new

@link = @konkurrancer.build_link

然后在表单中使用它

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

或者在视图中创建一个新链接,如下所示:

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

I will either build the object in the new action of the controller

@konkurrancer = Konkurrancer.new

@link = @konkurrancer.build_link

and then use this in the form

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

or create a new link in the view like this:

<%= f.simple_fields_for :link, Link.new do |d| %>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文