我有一个Ruby on Rails页面来编辑用户个人资料。表单正确显示用户名和电子邮件。当我在文本框中更改名称并单击更新时,我最终返回edit_profile页面,并显示消息“配置文件已成功更新”。问题是我将名称更改为未保存到数据库的值。
没有错误,服务器输出中的参数看起来是正确的。
处理UsersController #update(for 127.0.0.1 at 2009-05-19 22:00:48)[PUT]参数: {“user”=> {“name”=>“新名称”, “电子邮件”=> “中test@test.com”}, “提交”=> “中更新”, “action”=>“update”,“_ method”=>“put”, “authenticity_token”=> “中59c79fa90aaf5558aaab8cddef6acb7a4c7c55c3”, “id”=>“1”,“controller”=>“users”}
我错过了什么?
edit_profile.html.erb
<% form_for @profile, :url => {:action => "update", :id => @profile} do |f| %>
<%= f.error_messages %>
<p>Name: <%= f.text_field :name %></p>
<p>Email: <%= f.text_field :email %></p>
<%= f.submit "Update" %>
<% end %>
users_controller.rb
def edit_profile
@profile = User.find(current_user.id)
end
def update
@profile = User.find(params[:id])
respond_to do |format|
if @profile.update_attributes(params[:profile])
flash[:notice] = 'Profile was successfully updated.'
format.html { render :action => 'edit_profile' }
else
flash[:notice] = 'Profile Error.'
format.html { render :action => "edit_profile" }
end
end
end
编辑: 是的,这是一个命名问题,为了解决这个问题,我改变了......
if @profile.update_attributes(params[:profile])
到
if @profile.update_attributes(params[:user])
答案 0 :(得分:1)
您的字段名称与传递给update_attributes方法的名称不匹配。
检查表单字段的名称(使用firebug),并且将是“name”等。
但是你传递的是一个名为“params”的数组:
update_attributes(params[:profile])
您的表单字段应称为“个人资料[名称]”,以使其正常工作。
如果您检查development.log,您应该会看到没有调用任何更新。