gpt4 book ai didi

ruby-on-rails - 编辑表单路由 - 在更新提交时运行#show 方法而不是#update

转载 作者:行者123 更新时间:2023-12-04 06:03:35 24 4
gpt4 key购买 nike

我有一个带有虚 url 的应用程序,我一直在努力允许用户更新属性。编辑表单加载正确,但提交表单后,rails 没有运行更新方法,而是重新路由到根。不确定为什么会这样....

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

最佳答案

@比尔灵顿。您的回答非常接近,让我找到了解决方法。定义方法并没有做到这一点,但是定义了 Action 。

<%= form_for @user, :url => { :action => "update" } do |form| %> 

出于某种原因,这成功了……不确定为什么没有将操作定义为更新。

关于ruby-on-rails - 编辑表单路由 - 在更新提交时运行#show 方法而不是#update,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4611690/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com