【发布时间】:2015-06-17 16:46:27
【问题描述】:
我有一个工具模型,我希望能够同时拥有一个常规工具索引 (tools_path),它列出了数据库中所有用户的所有工具,还有一个用户工具索引 (users_tools_path),它列出了所有工具特定的用户。
我不确定 Rails 的实现方式是什么。我正在使用设计,我的直觉是在我的路线中执行以下操作:
Rails.application.routes.draw do
devise_for :users
root 'tools#index'
resources :users do
resources :tools
end
resources :tools
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 / tools#index
user_tools GET /users/:user_id/tools(.:format) tools#index
POST /users/:user_id/tools(.:format) tools#create
new_user_tool GET /users/:user_id/tools/new(.:format) tools#new
edit_user_tool GET /users/:user_id/tools/:id/edit(.:format) tools#edit
user_tool GET /users/:user_id/tools/:id(.:format) tools#show
PATCH /users/:user_id/tools/:id(.:format) tools#update
PUT /users/:user_id/tools/:id(.:format) tools#update
DELETE /users/:user_id/tools/:id(.:format) tools#destroy
users GET /users(.:format) users#index
POST /users(.:format) users#create
new_user GET /users/new(.:format) users#new
edit_user GET /users/:id/edit(.:format) users#edit
user GET /users/:id(.:format) users#show
PATCH /users/:id(.:format) users#update
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy
tools GET /tools(.:format) tools#index
POST /tools(.:format) tools#create
new_tool GET /tools/new(.:format) tools#new
edit_tool GET /tools/:id/edit(.:format) tools#edit
tool GET /tools/:id(.:format) tools#show
PATCH /tools/:id(.:format) tools#update
PUT /tools/:id(.:format) tools#update
DELETE /tools/:id(.:format) tools#destroy
这是我的工具控制器:
class ToolsController < ApplicationController
before_action :set_tool, only:[:show, :edit, :update, :destroy]
before_action :authenticate_user!, only:[:new, :destroy, :edit], notice: 'you must be logged in to proceed'
def index
@tools = Tool.all
end
def show
end
def new
@user = current_user
@tool = @user.tools.build
end
def create
@tool = Tool.new(tool_params)
@tool.save
redirect_to @tool
end
def edit
end
def update
@tool.update(tool_params)
redirect_to @tool
end
def destroy
@tool.destroy
redirect_to tools_path
end
private
def set_tool
@tool = Tool.find(params[:id])
end
def tool_params
params.require(:tool).permit(:name, :description)
end
end
这些是我的模型:
class Tool < ActiveRecord::Base
belongs_to :user
end
class User < ActiveRecord::Base
has_many :tools
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
end
不幸的是,当我导航到users/id/tools 时,会列出数据库中的所有工具,而不仅仅是该特定用户的工具。此外,当我查看数据库中每个工具的活动记录实例时,user_id 列是 nil
但是我被困在这里。显然,每个索引的代码都会有所不同,因此我不能对两种情况或相同的 index.html.erb 视图页面使用相同的索引操作。
我想知道小麦下一步会是什么?
以下是我认为我可以做的几件事:
1) 在我的工具控制器中创建一个新操作,例如 user_index,并包含用户和工具逻辑以获取与特定用户关联的工具。我还必须使用视图代码创建一个新的user_index.html.erb 视图。然后我会删除嵌套资源并添加如下路由:match 'users/:id/tools' => 'tools#user_index', :via => get
2) 我可以删除独立的resources :tools 路由并在我的嵌套工具资源中添加get :user_index, :on => :collection。然后像解决方案 #1 一样向我的控制器添加一个 user_index 操作。这里唯一的问题是我所有的路由都会有我不想要的user/:id 前缀。
在这种情况下,最好的解决方案是什么?另外,为什么我的活动记录工具实例没有保存创建它们的用户的 ID?有没有办法让他们在不嵌套资源的情况下保存用户 ID?
这个的rails方式是什么?
【问题讨论】:
标签: ruby-on-rails ruby-on-rails-4 devise routing resources