【发布时间】: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