我正在创建一个简单的注册表单,而我的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")
当我提交(看起来像什么)有效表格时。我收到一个错误,我的名字字段不能为空,但是,它们不是。未能保存的参数明显存在并存在于我的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>
用户控制器
def show
@user = User.find(params[:id])
end
def new
@user = User.new
end
def create
@user = User.new(user_params)
if @user.save
flash[:success] = "Welcome to the APP NAME!"
redirect_to @user
else
render 'new'
end
end
HTML
<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>
保存“无效”用户后Params变量:
--- !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
答案 0 :(得分:1)
看起来你错过了强大的参数。 Ruby on Rails指南中有一个描述:http://guides.rubyonrails.org/action_controller_overview.html#strong-parameters
在控制器的create
操作中,将@user = User.new(user_params)
更改为@user = User.new(profile_params)
,并定义私有profile_params
方法:
def profile_params
params.require(:user).permit(:first_name, :last_name, :email, :password, :password_confirmation)
end
这将允许控制器实际将参数值传递给模型,以便创建用户。