【问题标题】:How To Create Action Items For Specific Customer In Rails如何在 Rails 中为特定客户创建操作项
【发布时间】:2014-12-30 03:22:56
【问题描述】:

Rails 新手。 OOP 新手。我有一个客户端和 action_item 模型。一个操作项(一个待办事项)有很多并且属于很多客户。一个客户,有许多行动项目。本质上:用户从客户端页面创建 TODO。

用户:使用 crud 创建一个客户(Crayola LLC,例如)。 然后,用户在客户的展示页面(Crayola LLC 的展示页面)上。 我的问题是,如何拥有: 用户能够为该客户创建操作项。示例:致电 Crayola,向他们出售升级产品)。

创建了名为 action_items_clients 的连接表,带有外键 client_id 和 action_item_id。然迁移。只是不知道如何促进为客户创建行动项目。就目前而言,可以在没有客户端的情况下创建操作项。这很简单。这是我对 Rails 的新手理解遇到障碍的地方。

动作项目控制器:

类 ActionItemsController

def index
    @action_items = ActionItem.all


end 

def new
    @action_items = ActionItem.new
end

def create
        @action_item = ActionItem.new(action_items_params)
        if @action_item.save
            redirect_to(:action => 'show', :id => @action_item.id)
            #renders client individual page
        else 
            redirect_to(:action => 'new')
        end
end

def edit
    @action_item = ActionItem.find(params[:id])
end

def update
    @action_item = ActionItem.find(params[:id])
    if @action_item.update_attributes(action_items_params)
        redirect_to(:controller => 'action_items', :action => 'show', :id => @action_item.id)
        flash[:notice] = "Updated"
        else 
            render 'new'

    end 
end


def show
    @action_item = ActionItem.find(params[:id])


end

def action_clients
    @action_clients = ActionItem.Client.new 
end



def delete
    @action_items = ActionItem.find(params[:id])
end

def destroy
    @action_items = ActionItem.find(params[:id]).destroy
    redirect_to(:controller => 'action_items', :action => 'index')
end

private 
def action_items_params
    params.require(:action_item).permit(:purpose, :correspondence_method, :know_person, :contact_name_answer, :additional_notes)
end

结束

客户端控制器

class ClientsController < ApplicationController

def index
@clients = Client.all
end

def new 
@client = Client.new
end

def create
@client = Client.new(clients_params)
    if @client.save
        redirect_to(:action => 'show', :id => @client.id)
        #renders client individual page
    else 
        redirect_to(:action => 'new')
    end

end 

def edit
@client = Client.find(params[:id])
end

def update
@client = Client.find(params[:id])
  if @client.update_attributes(clients_params)
  redirect_to(:action => 'show', :id => @client.id)
else 
  render 'edit'
 end
end

def show
@client = Client.find(params[:id])

end

def delete
@client = Client.find(params[:id])
end

def destroy
@client = Client.find(params[:id]).destroy
redirect_to(:controller => 'clients', :action => 'index')
end

private
def clients_params
params.require(:client).permit(:name)
end


end

为每个客户显示页面:

<div align="center"><h1> <%= @client.name %> </h1></div>

<ol><li><%= link_to('Enter Definition Mode', :controller => 'action_items', :action  => 'new', :id => @client.id) %></br></br></li> 

<li><%= link_to('Back to client List', :controller => 'clients', :action => 'index')  %> </li></br>

</ol>

【问题讨论】:

    标签: ruby-on-rails associations


    【解决方案1】:

    我这样做的方法是设置你的路由,以便 action_items 嵌套在客户端下,如下所示:

    # /clients/13/action_items
    resources :clients do
      resources :action_items
    end
    

    或者如果登录的用户是一个客户端或只有一个客户端,那么你可以跳过那个,只用resources :action_items。

    然后,如果您将用户定向到/clients/13/action_items,那么他们将点击action_items#index,而params[:client_id] 将被设置为13。您可以使用它来确定整个控制器中的 action_items。

    只要你有Client和ActionItem之间的关系设置:

    class Client < ActiveRecord::Base
      belongs_to :user
      has_and_belongs_to_many :action_items
    end
    class ActionItem < ActiveRecord::Base
      has_and_belongs_to_many :clients
    end
    

    将其范围限定为当前登录的用户可能也很好:

    class User < ActiveRecord::Base
      has_many :clients
    end
    

    但这取决于您希望事情如何运作。这可能是我构建事物的方式:

    class ActionItemsController < ApplicationController
      before_action :get_client
    
      def index
        @action_items = @client.action_items.all
      end
    
      def new
        @action_items = @client.action_items.new
      end
    
      def create
        @action_item = @client.action_items.new(action_items_params)
        if @action_item.save
          redirect_to(:action => 'show', :id => @action_item.id, :client_id => @client.id)
        else 
          redirect_to(:action => 'new')
        end
      end
    
      # and other actions....
    
      private
    
      def get_client
        @client = current_user.clients.find(params[:client_id])
      end
    end
    

    编辑(解决一些评论问题):

    如果 action_items 并不总是限定在客户端,它们可以同时存在于嵌套和非嵌套路由下:

    # /action_items
    resources :action_items
    
    resources :clients do
      # /clients/13/action_items
      resources :action_items
    end
    

    然后before_action 可以更通用一点,将owner 设置为客户端或用户本身(只要User 也has_and_belongs_to_many :action_items):

    class ActionItemsController < ApplicationController
      before_action :get_owner
    
      def index
        @action_items = @owner.action_items.all
      end
    
      # ... other stuff
    
      private
    
      def get_owner
        if params[:client_id].present?
          @owner = current_user.clients.find(params[:client_id])
        else
          @owner = current_user
        end
      end
    end
    

    您的重定向可能需要考虑它们是否来自嵌套页面,因此您可能在它们周围有一些类似的逻辑:

    def destroy
      item = @owner.action_items.find(params[:id])
      item.destroy
      if params[:client_id]
        redirect_to client_action_items_path(params[:client_id])
      else
        redirect_to action_items_path
      end
    end
    

    您的link_tos 也必须进行类似的更改,这是上述销毁操作的链接:

    <% if params[:client_id].present? %>
      <%= link_to 'Delete action item', client_action_item_path(params[:client_id], @action_item), :method => 'delete' %>
    <% else %>
      <%= link_to 'Delete action item', @action_item, :method => 'delete' %>
    <% end %>
    

    【讨论】:

    • 杀手 - 我将在今晚晚些时候实施此功能,并将其标记为已回答。谢谢你的帮助。并感谢您对回答问题的支持。在学习过程中超级有帮助。 ——事实上已经建立了关系。我马上看到的问题是,如果用户需要创建一个不直接与客户端绑定的操作项,该怎么办。该应用程序(解释)是针对中小型企业的行动项目创建程序。用户也可以为:操作(公司操作)创建操作项,在这种情况下,它不需要绑定到客户端。有道理?和建议?
    • "您可以使用它来确定整个控制器的 action_items 范围。" - 你到底是什么意思? -- 现在,我有 clients/42/action_items 将用户发送到 action_items#index - 这有效。
    • 另外 - 该操作项索引页面上的删除链接现在以某种方式指向操作项显示页面。这必须是一个路由问题。我没有改变其他任何东西。
    • 知道了 - 现在查看。会在一段时间内大惊小怪!
    猜你喜欢
    • 2011-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多