I have a problem with my form in Rails 3. The forms work if I create something, but if I try to update som开发者_如何学JAVAething, 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"=>"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)
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!
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.
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?
精彩评论