【问题标题】:How to have a 'model' index and then also a 'model' index for each user(models_path & user_models_path)如何为每个用户创建一个“模型”索引和一个“模型”索引(models_path & user_models_path)
【发布时间】: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' =&gt; 'tools#user_index', :via =&gt; get

2) 我可以删除独立的resources :tools 路由并在我的嵌套工具资源中添加get :user_index, :on =&gt; :collection。然后像解决方案 #1 一样向我的控制器添加一个 user_index 操作。这里唯一的问题是我所有的路由都会有我不想要的user/:id 前缀。

在这种情况下,最好的解决方案是什么?另外,为什么我的活动记录工具实例没有保存创建它们的用户的 ID?有没有办法让他们在不嵌套资源的情况下保存用户 ID?

这个的rails方式是什么?

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-4 devise routing resources


    【解决方案1】:

    只显示特定用户的工具,而不是这样做:

    @tools = Tool.all
    

    您可以创建一个实例变量,它是分配给特定用户的工具数组。例如:

    @user = User.find(params[:id])
    @tools = Tool.where(user_id: @user.id)
    

    这只会收集工具 user_id 与加载到显示视图中的用户的用户 ID 匹配的工具。

    然后,当您执行循环时,它会为该用户显示正确的工具。

    【讨论】:

      【解决方案2】:

      有了上面的答案,你可以使用:

      @tools = (@user.present?) ? @user.tools : Tool.all
      

      为了能够设置每个工具的用户,您必须编辑您的:``

      params.require(:tool).permit(:name, :description)

      包括用户

              params.require(:tool).permit(:name, :description, :user)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-04-16
        • 2015-09-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多