User#new中的NameError

时间:2018-10-23 20:05:35

标签: ruby-on-rails

我有一个名为User的模型,正在以表单的形式引用它。形式如下:

<%= form_with(model: user, local: true) do |f| %>
<p>Who is the participant?</p>
<div class="field">
<%= f.label :person %>
<%= f.text_field :input %>
</div>
<p>What gifts would they like?</p>
<div class="field">
<%= f.label :gifts %>
<%= f.text_field :gifttext %>
 </div>
 <p>Who, if anyone, is their spouse? If they don't have one you can leave 
 this field blank</p>
 <div class="field">
 <%= f.label :person %>
<%= f.text_field :input %>
</div>
<p>What gifts would they like? If there is no spouse then you can leave this 
field blank.</p>
<div class="field">
<%= f.label :gifts %>
<%= f.text_field :gifttext %>
 </div>
<div class="actions">
 <%= f.submit %>
 </div>
 <%= link_to 'Done', user_results_path(@user) %>
 <% end %>

然后我得到这个错误:

NameError in User#new Showing C:/Sites/sstwo/app/views/user/_form.html.erb where line #1 raised:

undefined local variable or method `user' for
#<#<Class:0x5590668>:0x41b5ff0> Did you mean?  @user Extracted source (around line #1): 1 2 3 4 5 6

我确信这是一个快速修复,所以我愿意学习。如果需要更多详细信息,请告诉我。

1 个答案:

答案 0 :(得分:2)

在第一行中,您正在使用user,应为@user

form_with(model: @user, local: true)

来源:https://m.patrikonrails.com/rails-5-1s-form-with-vs-old-form-helpers-3a5f72a8c78a