2016-09-04 1 views
0

Я пытаюсь перейти на Restful route при добавлении/обновлении отношения соединения.Как использовать форму для обновления отношений соединения с вложенным маршрутом в Rails

У меня есть следующие контроллеры:

home_controller, stores_controller, user_stores_controller (join table)

С моей simpleform, Я обновляю любимый магазин пользователя (обновление объединения отношений). Тем не менее, маршрут принимает меня user_controller (которого у меня нет/не думал, что мне нужно, потому что devise обрабатывает моих пользователей). Я смущен тем, какой путь взять. Какой маршрут более ретентуален, и если мое мышление верное, путь для создания любимых магазинов пользователя должен быть через users_stores_controller с путём user/:user_id/stores. Другая возможность - обновить с помощью пути исправления /user/:user_id/stores/:id, но я не был уверен в этом, потому что я не просто обновляю одно хранилище, пользователь может выбрать несколько магазинов с атрибутом params store_ids будет передано

Или мне нужно обновлять на самом деле пользователь использует user_controller с почтовым контуром /user?

Что будет выглядеть 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 %> 

Вот мои модели:

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 

Вот мои маршруты:

конфигурации/маршруты:

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

    resources :user do 
    resources :stores 
    end 
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| %> 
Смежные вопросы