Rails 3表单在提交表单时使用“编辑”而不是“更新”

时间:2011-09-13 13:58:13

标签: ruby-on-rails-3 methods form-for

我在Rails 3中遇到了我的表单问题。如果我创建了某些内容,表单会起作用,但如果我尝试更新某些内容,则会失败。在这个例子中,我尝试更新用户设置,如姓名,邮件等。以下是相关的代码片段:

从User_Controller.rb编辑和更新

  def edit
    @title = "Nutzerverwaltung"
  end

  def update
    @user = User.find(params[:id])

    if @user.update_attributes(params[:user])
      flash[:success] = "Profil aktualisiert"
      redirect_to @user
    else
      @title = "Edit"
      render 'edit'
    end
  end

然后,有以下形式:

%form.user_edit
  = form_for @user, :url => {:controller => "users", :action => "update"}, :html => { :method => :put } do |f|
    = render 'shared/error_messages', :object => f.object
    = render 'fields', :f => f
    .actions
      = f.submit "Update"

运行本地WEBrick Server时的终端输出(某些表单标签有德语名称):

Started GET "/users/2/edit?utf8=%E2%9C%93&_method=put&authenticity_token=R5LfeIAjpJOpH%2B0yMD8PLO24%2Fgcct0CCqXuzoLoVibs%3D&user%5Banrede%5D=&user%5Bname%5D=Johnny&user%5Bemail%5D=t.schneider%40mail.com&user%5Bpassword%5D=&user%5Bpassword_confirmation%5D=&user%5Bplz%5D=&user%5Bort%5D=&user%5Bstrasse%5D=&commit=Update" for 127.0.0.1 at 2011-09-13 15:53:24 +0200
  Processing by UsersController#edit as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"R5LfeIAjpJOpH+0yMD8PLO24/gcct0CCqXuzoLoVibs=", "user"=>{"anrede"=>"", "name"=>"TESTer", "email"=>"t.schneider@mail.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "plz"=>"", "ort"=>"", "strasse"=>""}, "commit"=>"Update", "id"=>"2"}
  User Load (1.2ms)  SELECT "users".* FROM "users" WHERE ("users"."id" = 2) LIMIT 1
  CACHE (0.0ms)  SELECT "users".* FROM "users" WHERE ("users"."id" = 2) LIMIT 1
Rendered shared/_error_messages.html.haml (2.2ms)
Rendered users/_fields.html.haml (7.5ms)
Rendered layouts/_stylesheets.html.haml (2.4ms)
Rendered layouts/_header.html.haml (3.9ms)
Rendered layouts/_footer.html.haml (2.0ms)
Rendered users/edit.html.haml within layouts/application (33.2ms)
Completed 200 OK in 118ms (Views: 36.7ms | ActiveRecord: 1.2ms)

然后有我的佣金路线,对我来说似乎没问题:

       users GET    /users(.:format)          {:action=>"index", :controller=>"users"}
       users POST   /users(.:format)          {:action=>"create", :controller=>"users"}
    new_user GET    /users/new(.:format)      {:action=>"new", :controller=>"users"}
   edit_user GET    /users/:id/edit(.:format) {:action=>"edit", :controller=>"users"}
        user GET    /users/:id(.:format)      {:action=>"show", :controller=>"users"}
        user PUT    /users/:id(.:format)      {:action=>"update", :controller=>"users"}
        user DELETE /users/:id(.:format)      {:action=>"destroy", :controller=>"users"}
    sessions POST   /sessions(.:format)       {:action=>"create", :controller=>"sessions"}
 new_session GET    /sessions/new(.:format)   {:action=>"new", :controller=>"sessions"}
     session DELETE /sessions/:id(.:format)   {:action=>"destroy", :controller=>"sessions"}
       posts POST   /posts(.:format)          {:action=>"create", :controller=>"posts"}
   edit_post GET    /posts/:id/edit(.:format) {:action=>"edit", :controller=>"posts"}
        post DELETE /posts/:id(.:format)      {:action=>"destroy", :controller=>"posts"}
        root        /(.:format)               {:controller=>"pages", :action=>"start"}
                    /(.:format)               {:controller=>"pages", :action=>"start"}
       start        /start(.:format)          {:controller=>"pages", :action=>"start"}
     kontakt        /kontakt(.:format)        {:controller=>"pages", :action=>"kontakt"}
    tagebuch        /tagebuch(.:format)       {:controller=>"pages", :action=>"tagebuch"}
 hinzufuegen        /hinzufuegen(.:format)    {:controller=>"pages", :action=>"hinzufuegen"}
   bluefocus        /bluefocus(.:format)      {:controller=>"pages", :action=>"bluefocus"}
    seminare        /seminare(.:format)       {:controller=>"pages", :action=>"seminare"}
    angebote        /angebote(.:format)       {:controller=>"pages", :action=>"angebote"}
    specials        /specials(.:format)       {:controller=>"pages", :action=>"specials"}
        shop        /shop(.:format)           {:controller=>"pages", :action=>"shop"}
registrieren        /registrieren(.:format)   {:controller=>"users", :action=>"registrieren"}
       login        /login(.:format)          {:controller=>"sessions", :action=>"login"}
      logout        /logout(.:format)         {:controller=>"sessions", :action=>"destroy"}
                    /posts/:id/edit(.:format) {:controller=>"posts", :action=>"edit"}

我知道这个编辑功能曾经工作一次,但我不知道发生了什么变化,所以它不再起作用了。

如果您需要更多信息,请说一句话。

欢迎任何帮助。谢谢!

2 个答案:

答案 0 :(得分:0)

看起来update_attributes返回False并导致您的编辑操作被渲染。您的用户模型中存在哪些attr_accessible属性?

另外:存在哪些验证?

答案 1 :(得分:0)

您的HAML中有一个%form.user_edit标记,紧接在生成表单标记的form_for之前。由于您没有指定要提交表单的URL(在手动创建的表单标记中),因此默认情况下可能会将其发布到当前URL(可能是您的用户编辑路径)。

尝试删除%form.user_edit并将:html => {:class => 'user_edit', :method => :put}添加到form_for来电。