[英]Rails: How do you access model and controller data across views?
I am creating an app to vote on your favorite lunch, and I'm reusing code that I learned from a tutorial (railstutorial.org.) I have a relatively simply hierarchy within my data models, which look like this: 我正在创建一个应用程序来投票给您最喜欢的午餐,并且我正在重复使用从教程(railstutorial.org)中学习到的代码。我的数据模型中的层次结构相对简单,如下所示:
Cuisine model: 美食模型:
has_many :providers
validates :name, presence: true, uniqueness: true
Cuisine has the following columns: 菜系包括以下几列:
:id, :name
Provider model: 提供者模型:
belongs_to :cuisine
has_many :lunches
validates :name, presence: true, uniqueness: true
validates :cuisine, presence: true
which has the following attributes: 具有以下属性:
:id, :name, :cuisine_id
Lunch model: 午餐模式:
belongs_to :provider
default_scope -> { order('created_at DESC') }
validates :date, presence: true, uniqueness: true
Which has the following attributes: 具有以下属性:
:id, :date, :liked, :disliked, :provider_id, :lunchscore
A pretty straight forward inheritance model, to my untrained eye. 对于我未经训练的人来说,这是一个非常直接的继承模型。 What I am looking to do is list out all the different lunches that are associated with a provider, and highlight their score.
我要做的是列出与提供者相关的所有不同午餐,并突出显示他们的得分。
Provider Controller: 提供者控制器:
def show
@provider = Provider.find(params[:id])
@lunches = @provider.lunches
@cuisine = @provider.cuisine
end
In my Provider show.html.erb view , I have the following basic code: 在我的提供者show.html.erb视图中 ,我具有以下基本代码:
<div class="title"><%= @provider.name.upcase %> </div> <div class="cuisine"> <%= @cuisine.name %> </div>
<div class="span8">
<% if @provider.lunches.any? %>
<h3> Total Lunches - (<%= @provider.lunches.count %>) </h3>
<%= render @lunches %>
<% end %>
</div>
I also have a partial in the lunches folder called _lunch.html.erb which contains the following: 我在午餐文件夹中也有一个名为_lunch.html.erb的文件夹,其中包含以下内容:
<ul>
<span class="date_list"> <%= lunch.date %> - <%= @provider.name %> </span>
<span class="timestamp">Created <%= time_ago_in_words(lunch.created_at) %> ago. </span>
</span>
</ul>
This outputs the following to the page: 这将向页面输出以下内容:
Total Lunches - (1) 2015-01-24 - Sophies Created 2 days ago. 总午餐-(1)2015-01-24-Sophies在2天前创建。
Whilst this works (and that's great) I'm scratching my head trying to understand the behavior. 尽管这行得通(而且很棒),但我还是努力理解这种行为。 Specifically:
特别:
Why put this partial in the lunches folder (as it is currently) instead of the provider folder? 为什么将此部分放在午餐文件夹中(如当前)而不是提供者文件夹中? Does it have to be in the views/lunches folder?
是否必须在views / lunches文件夹中?
Why is the render call written like an instance variable <%= render @lunches %>
vs. specifying a folder, eg <%= render lunches/lunch %>
? 为什么以类似于实例变量
<%= render @lunches %>
编写render调用,而不是指定一个文件夹,例如<%= render lunches/lunch %>
<%= render @lunches %>
<%= render lunches/lunch %>
? Why doesn't the path need to be declared at all? 为什么根本不需要声明路径? how is rails transcribing @lunches into _lunch.html.erb?
Rails如何将@lunches转录为_lunch.html.erb?
Do I need to be rendering a partial in the lunches subfolder at all, or is there an easier way to access lunch data in a provider view? 我是否需要在午餐子文件夹中全部渲染一部分,还是在提供者视图中有一种更简单的方法来访问午餐数据?
Thanks in advance for likely a simple question. 在此先感谢您提出一个简单的问题。 My app is working, I'm just trying to understand the behavior.
我的应用程序正在运行,我只是想了解其行为。
When you try to render an active record object by it self, rails converts that object into a partial by calling to_partial_path
, which is by default the view/models/_model
path, you can test it by calling something like Lunch.new.to_partial_path
当您尝试通过自身呈现活动记录对象时,rails通过调用
to_partial_path
(默认情况下是view/models/_model
路径)将该对象转换为部分对象,您可以通过调用Lunch.new.to_partial_path
东西对其进行测试。
if you try render a single lunch: render @lunch
it would be equivalent to 如果您尝试渲染一顿午餐:
render @lunch
相当于
render partial: 'lunches/lunch', locals: {lunch: @lunch}
I didn't know it supported using a collection render @lunches
, but since you said it works so i guess it does, in this condition the equivalent would be something like 我不知道使用集合
render @lunches
支持它,但是由于您说过它可以工作,所以我想它可以工作,在这种情况下,等效项类似于
render partial: 'lunches/lunch', collection: @lunches, as: :lunch
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.