整页为/加部分

时间:2011-11-10 21:50:45

标签: ruby-on-rails partial

我想要一个页面是一个完整的页面,即我可以用这样的URL来调用它,例如:

/path/to/client/33

但我希望能够从另一个页面调用/渲染它,即(伪代码)

/path/to/customer/678
...find client id.....
render :client, :id => 33 #inside the customer page
render :customer

我明白我可以像偏爱那样做,我想不出的是如何在它们之间传递参数。 我也考虑过布局,但是我如何安排客户控制器首先运行?这样我就可以找到客户端ID,然后渲染客户端。 将客户视为“拥有”许多客户的一个实体。

1 个答案:

答案 0 :(得分:0)

# app/views/your_controller/_client.html.erb
The Client is <%= client.name %>

# app/views/your_controller/customer.html.erb
The Customer is <%= @customer.name %>
<%= render :partial => 'client', :locals => { :client => @client } %>

# app/views/your_controller/client.html.erb
<%= render :partial => 'client', :locals => { :client => @client } %>

# your_controller.rb
def customer
  @customer = Customer.find(params[:id])
  @client = @customer.client
end

def client
  @client = Client.find(params[:id])
  # i forget if you need to assign it client to make the partial work
  client = @client
end