2016-09-04 5 views
0

結合関係を追加/更新するときに安静なルートに移動しようとしています。フォームを使用してRailsでネストされたルートとの結合関係を更新する方法

私は以下のコントローラがあります。私のsimpleformで

home_controller, stores_controller, user_stores_controller (join table)

を、私は、ユーザーのお気に入りの店(結合関係の更新)を更新しています。しかし、ルートにはuser_controllerが必要です(deviseが自分のユーザーを処理しているため、必要ないと思っていませんでした)。私はどのルートをとるべきか混乱しています。どのルートがもっと安いですか。私の思考が正しいとすれば、ユーザーのお気に入りの店舗を作成するルートはusers_stores_controllerで、投稿パスはuser/:user_id/storesです。もう1つの可能性は/user/:user_id/stores/:idのパッチパスで更新されていますが、私はちょうど1つのストアを更新するだけではないので、ユーザーはstore_idsのparams属性を持つ複数のストアを選択できます。

実際のユーザーは、投稿パスが/userのuser_controllerを使用していますか?

simple_form_forの外観も同様ですか?ここで

は私のフォームである:ここでは

<%= simple_form_for(@user, html: { class: 'form-horizontal' }) do |f| %> 
    <%= f.association :stores, as: :check_boxes %> 
    <%= f.button :submit, "Update Favorite Stores", class: "btn btn-primary" %> 
<% end %> 

は私のモデルです:

設定/ルート:

Rails.application.routes.draw do 
    devise_for :users 
    root 'home#index' 

    resources :user do 
    resources :stores 
    end 
end 

Railsのルートここ

class User < ApplicationRecord 
    has_many :user_stores 
    has_many :stores, through: :user_stores   
end 

class UserStore < ApplicationRecord 
    belongs_to :user 
    belongs_to :store 
end 

class Store < ApplicationRecord 
    has_many :user_stores 
    has_many :users, through: :user_stores 
end 

は私のルートです

    Prefix Verb URI Pattern        Controller#Action 
     new_user_session GET /users/sign_in(.:format)     devise/sessions#new 
      user_session POST /users/sign_in(.:format)     devise/sessions#create 
    destroy_user_session DELETE /users/sign_out(.:format)    devise/sessions#destroy 
      user_password POST /users/password(.:format)    devise/passwords#create 
     new_user_password GET /users/password/new(.:format)   devise/passwords#new 
     edit_user_password GET /users/password/edit(.:format)   devise/passwords#edit 
         PATCH /users/password(.:format)    devise/passwords#update 
         PUT /users/password(.:format)    devise/passwords#update 
cancel_user_registration GET /users/cancel(.:format)     devise/registrations#cancel 
     user_registration POST /users(.:format)       devise/registrations#create 
    new_user_registration GET /users/sign_up(.:format)     devise/registrations#new 
    edit_user_registration GET /users/edit(.:format)     devise/registrations#edit 
         PATCH /users(.:format)       devise/registrations#update 
         PUT /users(.:format)       devise/registrations#update 
         DELETE /users(.:format)       devise/registrations#destroy 
        root GET /          home#index 
      user_stores GET /user/:user_id/stores(.:format)   stores#index 
         POST /user/:user_id/stores(.:format)   stores#create 
      new_user_store GET /user/:user_id/stores/new(.:format)  stores#new 
     edit_user_store GET /user/:user_id/stores/:id/edit(.:format) stores#edit 
       user_store GET /user/:user_id/stores/:id(.:format)  stores#show 
         PATCH /user/:user_id/stores/:id(.:format)  stores#update 
         PUT /user/:user_id/stores/:id(.:format)  stores#update 
         DELETE /user/:user_id/stores/:id(.:format)  stores#destroy 
       user_index GET /user(.:format)       user#index 
         POST /user(.:format)       user#create 
       new_user GET /user/new(.:format)      user#new 
       edit_user GET /user/:id/edit(.:format)     user#edit 
        user GET /user/:id(.:format)      user#show 
         PATCH /user/:id(.:format)      user#update 
         PUT /user/:id(.:format)      user#update 
         DELETE /user/:id(.:format)      user#destroy 

答えて

0

単純な形式でURLを指定してください:

<%= simple_form_for(@user, url: user_stores_path, html: { class: 'form-horizontal' }) do |f| %> 
関連する問題