2012-10-06 17 views
0

我有兩種模式供用戶使用;計劃模型has_many用戶。現在,我想要做的是允許用戶通過更改plan_id來升級/降級他們的計劃。我已經設置了一個表單以及適當的操作,但是當我點擊提交時,它似乎沒有執行PUT操作所說的內容。它似乎使用更新操作。表格發送到錯誤的PUT請求

這裏是我的形式:

     <%= form_tag("https://stackoverflow.com/users/update_plan", :method => "put") do %> 

         <%= hidden_field_tag :plan_id, plan.id %> 

         <%= submit_tag("Change To Plan", :class => "signup") %> 


         <% end %> 

這裏是我的更新動作

def update_plan 
    @user = current_user 
    @user.plan_id = params[:plan_id] 
    @user.save 
    sign_in @user 
    redirect_to change_plan 
    end 

當我提交以上,雖然,它不僅不辦理變更登記的形式,但我認爲它使用更新操作,而不是update_plan操作。我認爲這是因爲它重定向到更新操作中的內容,並且它與更新操作一樣閃爍。

def update 
    @user = current_user 
    if @user.update_attributes(params[:user]) 
     flash[:success] = "Profile updated" 
     sign_in @user 
     redirect_to edit_user_path(@user) 
    else 
     render 'edit' 
    end 
    end  

這是我的routes.rb文件

Dentist::Application.routes.draw do 

    resources :users 
    resources :sessions, only: [:new, :create, :destroy] 
    resources :phones, only: [:new, :create, :destroy] 
    resources :find_numbers, only: [:new, :create, :destroy] 

    put 'users/update_plan' 
    match '/signup', to: 'users#new' 
    match '/login', to: 'sessions#new' 
    match '/signout', to: 'sessions#destroy', via: :delete 
    match '/change_plan', to: 'users#change_plan' 

    root to: 'static_pages#home' 

    match '/product_demo', to: 'static_pages#product_demo' 

    match '/pricing', to: 'plans#index' 

    match '/contact', to: 'static_pages#contact' 

而這裏所發生的事情的控制檯截圖:

http://stepanp.com/debug3.jpg

這似乎說這是用UPDATE_PLAN行動,但。 ..:S

在試圖讓UPDATE_PLAN訴權任何幫助ñ功能將不勝感激!

回答

1

形式是要在正確的地方(/用戶/ UPDATE_PLAN),但正在被路由到:因爲它說,在你的控制檯日誌的第二行

UsersController#update 

。所以不是你期望的行爲,問題出在你的路線上。試試這個,列出你所有的路線:

rake routes 

也許用戶更新路線(由資源創建:用戶)正趕上這首:

PUT /users/:id(.:format)         users#update 

上有ID的內容沒有任何限制,格式是可選的,所以users/update_plan會用update_plan的id調用用戶/更新(實際上你可以看到這發生在控制檯日誌截圖的邊緣,查找:id ​​=>參數)。

因此,我將您的自定義路線移動到頂部的航線先向上述資源:用戶,也嘗試改變它引導到你想要的動作,不知道什麼規定沒有采取行動的路線呢?

put '/users/update_plan', to: 'users#update_plan' 
+0

A-和你是驚人的先生!謝謝你的解釋,它工作:)。 –

相關問題