我有一个带虚荣网址的应用程序,我一直在努力让用户更新属性。编辑表单正确加载,但在提交表单后,rails重新路由到root,而不是运行更新方法。不确定为什么会发生这种情况......
# users_controller
def edit
@user = User.find(params[:id])
end
def to_param # overridden
username.parameterize
end
def update
@user = User.find(params[:id])
if @user.update_attributes(params[:user])
redirect_to user_url(current_user.username), :flash => { :success => "success" }
else
redirect_to user_url(current_user.username), :error => { :error => "shit" }
end
end
路由
resources :users do
resources :friends
end
match '/:username' => 'users#show', :as => "user"
形式
<%= form_for @user do |form| %>
<%= render 'shared/error_messages', :object => form.object %>
<div class="form">
<p> <%= form.label :description, "Message to friends" %> <br />
<%= form.text_area :description %> </p>
<%= form.submit %>
</div>
<% end %>
开发日志
Started GET "/users/1/edit" for 127.0.0.1 at Wed Jan 05 19:07:28 -0500 2011
Processing by UsersController#edit as HTML
Parameters: {"id"=>"1"}
User Load (0.2ms) SELECT "users".* FROM "users" WHERE ("users"."id" = 1) LIMIT 1
Rendered shared/_error_messages.html.erb (0.6ms)
ApplicationController::current_user
ApplicationController::current_user_session
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE ("users"."id" = 1) LIMIT 1
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE ("users"."id" = 1) LIMIT 1
ApplicationController::current_user_session
ApplicationController::current_user
ApplicationController::current_user
Rendered users/edit.html.erb within layouts/application (27.6ms)
Completed 200 OK in 53ms (Views: 38.7ms | ActiveRecord: 0.3ms)
Started POST "/1" for 127.0.0.1 at Wed Jan 05 19:08:06 -0500 2011
Processing by UsersController#show as HTML
Parameters: {"commit"=>"Update User", "authenticity_token"=>"OM1lIzizuFCYlxC3XmtmG/btqAsyjekHtqsiwlUDn3M=", "utf8"=>"✓", "username"=>"1", "user"=>{"description"=>"Hello people! Give me your address. Get a postcard."}}
User Load (0.2ms) SELECT "users".* FROM "users" WHERE ("users"."username" = '1') LIMIT 1
Redirected to http://0.0.0.0:3000/
ApplicationController::current_user
ApplicationController::current_user_session
User Load (0.3ms) SELECT "users".* FROM "users" WHERE ("users"."id" = 1) LIMIT 1
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE ("users"."id" = 1) LIMIT 1
ApplicationController::current_user_session
Completed 302 Found in 102ms
相关的佣金路线:
users GET /users(.:format) {:controller=>"users", :action=>"index"}
POST /users(.:format) {:controller=>"users", :action=>"create"}
new_user GET /users/new(.:format) {:controller=>"users", :action=>"new"}
edit_user GET /users/:id/edit(.:format) {:controller=>"users", :action=>"edit"}
user GET /users/:id(.:format) {:controller=>"users", :action=>"show"}
PUT /users/:id(.:format) {:controller=>"users", :action=>"update"}
DELETE /users/:id(.:format) {:controller=>"users", :action=>"destroy"}
答案 0 :(得分:2)
@Beerlington。你的答案非常接近,导致我找到了解决办法。定义方法并不是这样做,而是定义了操作。
<%= form_for @user, :url => { :action => "update" } do |form| %>
由于某种原因,这就成功了......不确定为什么行动没有被定义为更新。
答案 1 :(得分:1)
查看表单生成的HTML。我打赌这个动作看起来像
action="/1"
因此,在提交表单时,您的路径文件会显示
match '/:username' => 'users#show', :as => "user"
这就是你被发送的地方
Started POST "/1" for 127.0.0.1 at Wed Jan 05 19:08:06 -0500 2011
Processing by UsersController#show as HTML
我认为form_for依赖于to_param来为表单生成操作。因为你写了它,你就会出现意想不到的行为。
答案 2 :(得分:0)
您的代码对我来说很好,但是日志显示表单没有呈现包含“put”的隐藏_method
字段。这可能是由form_for帮助程序未将@user识别为现有记录引起的。我不知道为什么会发生这种情况,但临时解决方法是将以下内容添加到您的form_for帮助程序中:
:html => { :method => :put}