[英]Rails form_for and text_field without model attribute
I'm writing a form for users to register their products with. 我正在写一个表格供用户注册产品。 I already have
class User has_many :products
and class Product belongs_to :user; attr_accessible :serial_number
我已经有
class User has_many :products
和class Product belongs_to :user; attr_accessible :serial_number
class Product belongs_to :user; attr_accessible :serial_number
. class Product belongs_to :user; attr_accessible :serial_number
。 So the idea is that the user enters the serial number in the form and I call @user.products << Product.find_by_serial_number params[:serial_number]
or somesuch. 因此,想法是用户以表格形式输入序列号,然后调用
@user.products << Product.find_by_serial_number params[:serial_number]
或类似的名称。
It seemed sensible to add this as a register action to the UsersController
: 将其作为注册动作添加到
UsersController
似乎是明智的:
resources :users do
member do
get 'register'
end
end
And I have the following in register.html.erb
: 我在
register.html.erb
具有以下内容:
5: <%= render 'shared/error_messages' %>
6:
7: <%= f.label :serial_number, "Serial number" %>
8: <%= f.text_field :serial_number %>
9:
10: <%= f.submit "Register product" %>
11: <% end %>
This throws the error: 这引发了错误:
undefined method `serial_number' for #<User:0x000000060b1d40>
extracted source (around line #8):
...
So...a) How do I stop this error? 所以... a)如何停止该错误? b) Umm...what's going to happen after the submit button is pressed?
b)嗯...按下“提交”按钮后会发生什么? I'm assuming it'll call the
update
action (which is also used by the edit form)...should I make that fatter to deal with both possiblities? 我假设它将调用
update
操作(编辑表单也使用该操作)...我应该更胖些来处理这两种可能性吗? Or make a new action? 还是采取新的行动? (If so, how do I reroute the form to point to that?) c) Error-handling (putting in an invalid serial number) is best handled in the controller, not the model, right?
(如果是这样,我该如何重新路由该表格以指向该表格?)c)错误处理(输入无效的序列号)最好在控制器而不是模型中处理,对吗?
Thanks. 谢谢。
You can only use the form helper for attributes of the model. 您只能将表单帮助器用于模型的属性。 In this case you will need to use the full method.
在这种情况下,您将需要使用完整方法。
= label_tag :serial_number, 'Serial number'
= text_field_tag :serial_number
The error being thrown indicates that there's no method serial_number
for the User
model... which there isn't. 引发的错误表明
User
模型没有方法serial_number
...没有。 The method is actually attached to the Product
model, but the form in register.html.erb
is for User
. 该方法实际上是附加到
Product
模型的,但是register.html.erb
的形式是User
。 You can resolve this by using a nested form. 您可以使用嵌套表单来解决此问题。
First, ensure that User
can accept nested attributes for Product
: 首先,确保
User
可以接受Product
嵌套属性:
# app/models/user.rb
has_many :products
accepts_nested_attributes_for :products
Then, make your form into a nested form: 然后,使您的表单成为嵌套表单:
# app/views/users/register.html.erb
<%= form_for(@user) do |f| %>
<%= f.fields_for :products do |e| %>
<%= e.label :serial_number, "Serial number" %>
<%= e.text_field :serial_number %>
<% end %>
<%= f.submit "Register product" %>
<% end %>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.