更新了w / dev日志。见下文
我在路由中使用了虚荣网址,我假设它与它有关... 简而言之,我的问题是我无法让用户更新他们的信息。我可以让编辑表单显示但是在提交时,更新方法没有运行,我也没有“失败”或“成功”。
相关路线如下:
resources :users do
resources :friends
end
match '/:username/edit' => 'users#edit', :as => "edit_user"
match '/:username' => 'users#show', :as => "user"
我的表单现在看起来像这样,但我尝试了几个不同的东西。
<%= form_for @user, :url => user_path(@user.username) 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 "Update" %>
</div>
<% end %>
编辑和更新控制器如下所示:
def edit
@user = User.find_by_username(params[:username])
end
def update
@user = User.find(params[:id])
if @user.update_attributes(params[:user])
redirect_to user_url(current_user.username), :flash => { :success => "wham" }
else
redirect_to user_url(current_user.username), :error => { :error => "shiz" }
end
end
目前,当提交更新表单时,您将被定向到用户URL,但没有任何反应。我很感激任何帮助。谢谢!
更新---- 我使用to_param更改了我的路由,如下所述。这工作得很好,但问题仍然存在,我的更新表单只是重定向到root_url。在rake路径下,我得到了正确的更新路径,并且编辑表单正确显示。以下是我从rake路线获得的信息:
PUT /users/:id(.:format) {:controller=>"users", :action=>"update"}
这是提交表单的开发日志
Started POST "/1" for 127.0.0.1 at Wed Jan 05 16:09:54 -0500 2011
Processing by UsersController#show as HTML
Parameters: {"commit"=>"Update User", "authenticity_token"=>"OM1lIzizuFCYlxC3XmtmG/btqAsyjekHtqsiwlUDn3M=", "utf8"=>"✓", "username"=>"1", "user"=>{"description"=>"Update the message please"}}
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.2ms) 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 44ms
Started GET "/" for 127.0.0.1 at Wed Jan 05 16:09:54 -0500 2011
Processing by PagesController#home as HTML
ApplicationController::current_user
ApplicationController::current_user_session
User Load (0.2ms) 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
ApplicationController::current_user
ApplicationController::current_user
Rendered pages/home.html.erb within layouts/application (22.2ms)
Completed 200 OK in 61ms (Views: 32.4ms | ActiveRecord: 0.2ms)
为什么会发生重定向?
答案 0 :(得分:2)
不要为此定义自定义路由,只需在to_param
类上定义User
方法:
def to_param
username.parameterize
end
然后,对于您的路线,params[:id]
值将是用户名的参数化版本。例如,我的将是ryan-bigg
。
答案 1 :(得分:0)
即使您的日志没有表明是这种情况,我也会检查用户模型中是否attr_accessible
添加了:description
。它发生在我之前 - 表单似乎有效但信息无法存储。