2011-01-06 33 views
3

我有一個應用程序與虛榮的URL,我一直在努力允許用戶更新屬性。編輯表單加載正確,但在提交表單之後,而不是運行更新方法,rails會重新路由到根目錄。也不清楚爲什麼這種情況正在發生....編輯表單路由 - 在更新提交運行#show方法,而不是#update

# 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 "https://stackoverflow.com/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

在您的應用程序目錄中運行「rake routes」並將輸出添加到您的問題中。 – ddayan 2011-01-06 04:36:56

+0

爲什麼控制器中的to_param方法? – 2011-01-06 04:49:47

+0

增加了耙路。 to_param方法使得用戶名替換id。請參閱http://api.rubyonrails.org/classes/ActiveRecord/Base.html#method-i-to_param – owilde1900 2011-01-06 04:57:00

回答

0

您的代碼對我來說看起來不錯,但日誌顯示窗體並未呈現包含「put」的隱藏_method字段。這可能是由form_for助手不會將@user識別爲現有記錄所致。我不知道爲什麼這可能發生,但臨時的解決辦法是將以下添加到您的form_for助手:

:html => { :method => :put} 
+1

這很接近,並導致我定義:url => {:action =>「update」},最終使其工作。 – owilde1900 2011-01-06 05:23:54

2

@Beerlington。你的回答非常接近,讓我找到了一個解決方案。定義該方法並沒有做到這一點,但是定義了這個行爲的是什麼。

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

由於某種原因,這樣做了......不知道爲什麼該操作未定義爲更新。

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來爲表單生成動作。既然你寫了它,你會得到意想不到的行爲。

+0

@monocole - 你完全正確。表單動作是「/ 1」,它確實試圖運行show方法。但是,我已經刪除了用戶控制器中的to_param以及activerecord,並且表單仍然以此方式運行。這是其他事情導致它這樣做.. – owilde1900 2011-01-06 05:32:33