简体   繁体   English

没有模型属性的Rails form_for和text_field

[英]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 :productsclass 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.

相关问题 Rails form_for,具有不带标签的text_field(不使用CSS) - Rails form_for, having a text_field without a label (without using CSS) 将glyphicon插入rails form_for text_field - Insert glyphicon inside rails form_for text_field 如何将ng-keyup添加到rails text_field helper for form_for - how to add ng-keyup to rails text_field helper for form_for 在form_for,rails中传递collection_selection和text_field值 - Passing a collection_selection and text_field values in a form_for, rails Rails 4如何使用其他text_field的复选框集合为表单建模 - Rails 4 How to model a form with a collection of checkboxes with other text_field form_for 中 text_field 和 text_area 的区别 - difference between text_field and text_area in form_for Rails形式的text_field转义html - Rails form text_field escaping html 输入字段方法(text_area,text_field等)如何从form_for块内的记录中获取属性值? - How do input field methods (text_area, text_field, etc.) get attribute values from a record within a form_for block? 如何设置text_field必需属性false以使用Ruby on Rails提交表单 - How to set text_field required attribute false to submit the form using Ruby on Rails Rails:如何在没有表单的情况下插入text_field来测试布局? - Rails: How to insert a text_field without a form just to test the layout?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM