我有一个简单的表格。
<%= simple_form_for @user, html: { multipart: true }, remote: true do |f| %>
<div class = "UserEditsJS">
<div class="row">
<div id = "NoPaddingFormShort" class = "col-lg-6 col-lg-offset-0 col-md-5 col-md-offset-1 col-sm-6 col-sm-6">
<%= f.text_field :first_name, class: 'form-control-small-short' %>
</div>
<div id = "NoPaddingFormShort" class = "col-lg-6 col-md-5 col-sm-6 col-sm-6">
<%= f.text_field :last_name, class: 'form-control-small-short' %>
</div>
</div>
</div>
更新操作
def update
respond_to do |format|
if @user.update(user_params)
if @user.save
flash.now[:success] = "Profile updated"
format.html { redirect_to @user }
format.js { render 'business_card'}
else
flash.now[:danger] = "There was a problem with your update"
format.html { redirect_to user_path(@user) }
format.js
end
end
end
我正在运行规范
it "current user tries to delete their first name" do
put(:update, :id => @user.id, :user => invalid_attributes)
@user.reload
expect(response).to redirect_to(@user), format: 'js'
expect(@user.first_name).to_not eql invalid_attributes[:first_name]
end
用户模型需要名字。
1) UsersController User updates current user tries to delete their first name
Failure/Error: put(:update, :id => @user.id, :user => invalid_attributes)
ActionController::UnknownFormat:
ActionController::UnknownFormat
# ./app/controllers/users_controller.rb:48:in `update'
# ./spec/controllers/users_controller_spec.rb:58:in `block (3 levels) in <top (required)>'
问题显然是格式
我试过了:
format.js { render js: 'business_card'}
和
format.js { render 'business_card'}
控制器中的以及其他几个选项。
用户何时成功更新所有工作的规范,而不是更新失败时的规范。在SO上有很多例子,但似乎没有解决这个具体问题。我确定这是一个单行答案....我只是看不到它。
答案 0 :(得分:2)
您可以尝试在请求中设置格式,如下所示:
put :update, :id => @user.id, :user => invalid_attributes, format: :js
或设置params hash params = { id: @user.id, user: invalid_attributes, format: :js }
并使用此put :update, params