2016-06-22 6 views
0

カスタムURLルートを持つ私のレールアプリにユーザーオブジェクトがあります。更新フォームを送信すると、何らかの理由でユーザーIDがURLに追加されていて、ルーティングエラーが発生しています。オブジェクトパラメータを更新するとURLにIDが追加され、エラーがスローされます

ルート:

get  'myaccount' => 'users#show', as: 'user' 
get  'myaccount/edit' => 'users#edit' 
patch 'myaccount/edit' => 'users#update' 
put  'myaccount/edit' => 'users#update' 

ビュー:

<%= form_for(@user) do |f| %> 
    <%= render 'shared/error_messages', :form_object => @user %> 

    <%= f.label :first_name %> 
    <%= f.text_field :first_name, class: 'form-control' %> 

    <%= f.label :last_name %> 
    <%= f.text_field :last_name, class: 'form-control' %> 


    <%= f.label :password %> 
    <%= f.password_field :password, class: 'form-control' %> 

    <%= f.label :password_confirmation %> 
    <%= f.password_field :password_confirmation, class: 'form-control' %> 

    <%= f.submit "Save changes", class: "btn btn-primary" %> 
<% end %> 

コントローラのアクション:

def update 
    @user = User.find_by(id: current_user.id) 
    if @user.update_attributes(user_params) 
     # successful update 
    else 
     # unsuccessful update 
    end 
    end 

エラー:提出時に は、URLが 'myaccount.7' で、私を得ますエラー: ルーティングエラー ルートなしm atches [PATCH] "/myaccount.7"

答えて

1

カスタムルートがあり、フォームを送信する場所と使用する方法が混乱します。明示的にurlmethodオプションを指定する必要があります。

<%= form_for(@user, url: 'myaccount/edit', action: :put) do |f| %> 

Docs.

+0

添加だけが持つ '/マイアカウント/編集' であるために必要なurl変数であります最初の文字 '/'、それ以外の場合はpo st URLは '/ myaccount/myaccount/edit'と表示されていました – Utopia025

0

このを試してみてください。

resources :myaccount, controller: 'users' 

出力:

myaccount_index GET /myaccount(.:format)                users#index 
             POST /myaccount(.:format)                users#create 
          new_myaccount GET /myaccount/new(.:format)               users#new 
          edit_myaccount GET /myaccount/:id/edit(.:format)              users#edit 
           myaccount GET /myaccount/:id(.:format)               users#show 
             PATCH /myaccount/:id(.:format)               users#update 
             PUT /myaccount/:id(.:format)               users#update 
             DELETE /myaccount/:id(.:format)               users#destroy 
             GET /myaccount(.:format)    
+0

これは、URLパスからIDのバールを削除するという私の本来の目的を達成していません。私はGET myaccount /:idだけを '/ myaccount'にして、 '/ myaccount/edit'ではなく '/ myaccount /:id/edit'になるように編集したいとします。 – Utopia025

+0

idなしでどのように識別できますか、編集または削除 – vipin

関連する問題