Rails 3.1一种模型的不同视图 [英] Rails 3.1 Different Views for One Model
问题描述
我有一个具有运货模型的应用程序.我创建了一个视图,该视图允许我创建发货并在基于表格的布局中查看发货.一切都在index.html.erb中,因为所有操作都是通过ajax完成的.
I have an application where I have a Shipment model. I have created a view that allows me to create shipments and view shipments in a table based layout. Everything lies in the index.html.erb because all actions are done through ajax.
创建货件后,默认状态为打开".稍后在打印提单时,状态将更改为已打印".
When a shipment is created, the status is "OPEN" by default. Later on when a bill of lading is printed, the status will change to "PRINTED".
我的目标是创建另一个仅列出未完成装运的视图,以便操作员使用单独的表格打印提货单.我的问题是,Rails如何处理一个模型的多种形式和视图的渲染?我应该使用单独的控制器/视图来处理与打印相关的动作/视图,还是应该使用两个模型进行防爆.出货和OpenShipment?我很难找到解释此问题的文档.
My goal is to create another view that lists only the open shipments allowing an operator to print the bill of lading using a separate form. My question is how does rails handle the rendering of multiple forms and views for one model? Should I use a separate controller/view to handle the action/views associated with printing, or should I use two models for ex. Shipment and OpenShipment? I'm having a hard time finding documentation that explains this issue.
下面列出的是我对此模型的架构以及当前视图的目录结构.希望这有助于解释问题.
Listed below is my schema for this Model as well as a directory structure for the current view. Hope this helps explain the issue.
Shipment Model
t.string :item_code
t.string :status
t.string :bill_of_lading
t.integer :qty_shipped
t.datetime :ship_date
t.datetime :delivery_date
查看目录结构
views
shipments
index.html.erb
_shipment.html.erb
_shipment_table.html.erb
_form.html.erb
_edit_form.html.erb
create.js.coffee
edit.js.coffee
update.js.coffee
destroy.js.coffee
推荐答案
如我所见,您将两次显示相同的内容.您可以通过两种方式解决此问题:要么添加参数,并保持相同的控制器动作,要么添加新的控制器动作(这可能会使URL更为简洁,具体取决于您的偏好).
As I see, you will be showing the same thing twice. You could solve this in two ways: either you add a parameter, and keep the same controller action, or you add a new controller action (which possibly makes a cleaner url --depends on your preference).
首先,您在Shipment
上定义scope
:
class Shipment
scope :open, lambda { where(:status => 'open')
end
这将允许编写如下内容:
This will allow to write something like:
Shipment.open
这将为您提供所有未清货.
and this will give you all the open shipments.
因此您可以按以下方式定义新的控制器动作
So you could define a new controller action as follows
def open_shipments
@shipments = Shipment.open
render :index
end
这将检索未完成的装运,然后呈现与索引相同的视图.
This will retrieve the open shipments, and then render the same view as for index.
要使其更加整洁,可以将state设置为url中的参数.
在您的config/routes.rb
中写
To make it even cleaner, you could make the state a parameter in your url.
In your config/routes.rb
write
get 'shipments/status/:status', :as => 'shipments#index'
然后,您将必须按以下步骤编辑index
动作:
And then you will have to edit your index
action as follows:
def index
if params[:status]
@shipments = Shipment.where(:status => params[:status])
else
@shipments = Shipment.all
end
end
然后您可以访问URL shipments/status/open
,它将提供未清的发货,同样,shipments/status/printed
也将提供所有已打印的发货.
And you can then just visit the url shipments/status/open
which will give the open shipments, and likewise shipments/status/printed
will give all the printed shipments.
但是URL shipments?status=open
将为您提供相同的结果(无需对必要的路由进行任何更改).取决于您的口味.
But the url shipments?status=open
will give you the same result (without any changes to the routes necessary). Depends on your flavour.
希望这会有所帮助.
这篇关于Rails 3.1一种模型的不同视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!