Ruby on rails RubyonRails为未写入模型的属性形成_,即使这些属性有效
我正在创建一个简单的注册表单,但是我的rspec验证失败了Ruby on rails RubyonRails为未写入模型的属性形成_,即使这些属性有效,ruby-on-rails,validation,rspec,form-for,Ruby On Rails,Validation,Rspec,Form For,我正在创建一个简单的注册表单,但是我的rspec验证失败了 (states: "Failure/Error: expect { click_button submit }.to change(User, :count).by(1) count should have been changed by 1, but was changed by 0") 当我提交一份有效的表格时。我得到一个错误,我的名字字段不能为空,但是,他们不是。无法保存的参数明显存在,并且存在于my params变量
(states: "Failure/Error: expect { click_button submit }.to change(User, :count).by(1)
count should have been changed by 1, but was changed by 0")
当我提交一份有效的表格时。我得到一个错误,我的名字字段不能为空,但是,他们不是。无法保存的参数明显存在,并且存在于my params变量中。我完全不知道这个问题是什么
注册页面:
<h1>Sign up</h1>
<div class="row">
<div class="span6 offset3">
<%= form_for(@user) do |f| %>
<%= render 'shared/error_messages' %>
<%= f.label :first_name %>
<%= f.text_field :first_name %>
<%= f.label :last_name %>
<%= f.text_field :last_name %>
<%= f.label :email %>
<%= f.text_field :email %>
<%= f.label :password %>
<%= f.password_field :password %>
<%= f.label :password_confirmation, "Confirmation" %>
<%= f.password_field :password_confirmation %>
<%= f.submit "Create my account", class: "btn btn-large btn-primary" %>
<% end %>
</div>
HTML
看起来您缺少强参数。Ruby on Rails指南中有一个描述: 在控制器的
create
操作中,将@user=user.new(user\u-params)
更改为类似@user=user.new(profile\u-params)
的内容,并定义私有profile\u-params
方法:
def profile_params
params.require(:user).permit(:first_name, :last_name, :email, :password, :password_confirmation)
end
这将允许控制器实际将参数值传递给要创建的用户的模型。在用户模型中,“name”的字段是“first_name”、“last_name”或仅仅是“name”?我有强参数,但其中包含的字段不正确,这就是无法识别它们的原因。谢谢
<div class="row">
<div class="span6 offset3">
<form accept-charset="UTF-8" action="/users" class="new_user" id="new_user" method="post">
<div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="✓" />
<input name="authenticity_token" type="hidden" value="HOESbh8pxffYQlmMDYOFm/fBWt1OXOz3CZ4mdgiXMUM=" />
</div>
<div id="error_explanation">
<div class="alert alert-error">
The form contains 4 errors.
</div>
<ul>
<li>* First name can't be blank</li>
<li>* First name is too short (minimum is 2 characters)</li>
<li>* Last name can't be blank</li>
<li>* Last name is too short (minimum is 2 characters)</li>
</ul>
</div>
<div class="field_with_errors"><label for="user_first_name">First name</label></div>
<div class="field_with_errors"><input id="user_first_name" name="user[first_name]" type="text" /></div>
<div class="field_with_errors"><label for="user_last_name">Last name</label></div>
<div class="field_with_errors"><input id="user_last_name" name="user[last_name]" type="text" /></div>
<label for="user_email">Email</label>
<input id="user_email" name="user[email]" type="text" value="baz@uitest.com" />
<label for="user_password">Password</label>
<input id="user_password" name="user[password]" type="password" />
<label for="user_password_confirmation">Confirmation</label>
<input id="user_password_confirmation" name="user[password_confirmation]" type="password" />
<input class="btn btn-large btn-primary" name="commit" type="submit" value="Create my account" />
</form>
</div>
</div>
--- !ruby/hash:ActionController::Parameters
utf8: ✓
authenticity_token: HOESbh8pxffYQlmMDYOFm/fBWt1OXOz3CZ4mdgiXMUM=
user: !ruby/hash:ActionController::Parameters
first_name: First name test
last_name: Last name test
email: baz@uitest.com
password: foobar
password_confirmation: foobar
commit: Create my account
action: create
controller: users
def profile_params
params.require(:user).permit(:first_name, :last_name, :email, :password, :password_confirmation)
end