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

发布于 2024-12-04 11:44:59 字数 5072 浏览 1 评论 0原文

我在 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 服务器时的我的终端输出(某些表单标签有德国名字):

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"=>"[email protected]", "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"}

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

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

欢迎每一个帮助。谢谢!

I have a problem with my form in Rails 3. The forms work if I create something, but if I try to update something, they fail. In this exmaple I tried to update the user settings, like name, mail etc. Here are the relevant code snippets:

Edit and Update from 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

Then, there is the form:

%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"

My terminal output when running the local WEBrick Server (some of the form labels have german names):

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"=>"[email protected]", "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)

And then there is my rake routes, which seems ok for me:

       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"}

I know that this function of editing was working once, but i don't know what changed, so that it doesn't work anymore.

If you need anymore information just say a word.

Every help is welcome. Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

绿光 2024-12-11 11:44:59

您的 HAML 中有一个 %form.user_edit 标记,位于生成表单标记的 form_for 之前。由于您没有指定要提交表单的 url(在您手动创建的表单标记中),因此它可能默认发布到当前 url(大概是您的用户编辑路由)。

尝试删除 %form.user_edit 并添加 :html =>; {:类=> 'user_edit', :方法 => :put} 到您的 form_for 调用中。

You have a %form.user_edit tag in your HAML, immediately before form_for which generates a form tag. Since you are not specifying the url to submit the form to (in the form tag you manually create), it is probably posting by default to the current url (presumably, your user edit route).

Try removing the %form.user_edit and adding :html => {:class => 'user_edit', :method => :put} to your form_for call.

梦里人 2024-12-11 11:44:59

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

另外:存在哪些验证?

It looks like update_attributes is returning False and causing your edit action to be rendered. What attr_accessible attributes exist on your user model?

Also: What validations exist?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文