【问题标题】:problem saving rails form问题保存轨道表格
【发布时间】:2009-05-20 02:04:51
【问题描述】:

我有一个 Ruby on Rails 页面来编辑用户个人资料。表单正确显示用户名和电子邮件。当我在文本框中更改名称并单击更新时,我最终回到了 edit_profile 页面,并显示消息“配置文件已成功更新。”。问题是我将名称更改为没有保存到数据库的值。

没有错误,服务器输出中的参数看起来正确。

处理用户控制器#update(对于 127.0.0.1 在 2009-05-19 22:00:48) [PUT] 参数: {"用户"=>{"名称"=>"新名称", "电子邮件"=>"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])

【问题讨论】:

    标签: ruby-on-rails


    【解决方案1】:

    您的字段名称与您传递给 update_attributes 方法的名称不匹配。

    检查表单字段的名称(使用firebug),将是“名称”等。

    但是您传递的是一个名为“params”的数组:

    update_attributes(params[:profile])  
    

    您的表单字段应命名为“profile[name]”以使其正常工作。

    如果您检查 development.log,您应该会看到没有调用任何更新。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-11-22
      • 1970-01-01
      • 2016-03-29
      • 1970-01-01
      • 1970-01-01
      • 2010-11-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多