Rails 表单帮助 has_one owns_to 关联
我正在尝试为链接创建一个简单的嵌套输入字段。
我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在您的 konkurrancer 模型中使用
in your konkurrancer model use
您希望传递给控制器的参数看起来像这样:
that's assuming that your Link model really has a
link
field.所以只需将其用于链接模型:
如有必要,请更改
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:
that's assuming that your Link model really has a
link
field.So just use this for the link model:
And change the
d.input :link
if necessary (i.e., if the Link table doesn't really have alink
field.)http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html
我将在控制器的新操作中构建对象
然后在表单中使用它
或者在视图中创建一个新链接,如下所示:
I will either build the object in the new action of the controller
and then use this in the form
or create a new link in the view like this: