如何向默认渲染方法传递一些参数?
我正在使用 Ruby on Rails 3.0.10,我想将一些参数传递给默认渲染方法。也就是说,如果我有这样的代码,
def show
...
respond_to do |format|
format.html # This, by default, renders the 'show.html.erb' file
end
end
我想传递一些参数,也许像(注意:以下不起作用),
def show
...
respond_to do |format|
# Here I would like to add some local objects that will be available in the 'show.html.erb' template file
format.html { render ..., :locals => { :content => { :value => 'Sample value' } } }
end
end
以便在 show.html.erb
模板中我可以做一些事情就像:
<%=
content[:value]
# => 'Sample value'
%>
简而言之,我想以与 :locals
键相关的部分模板渲染相同的方式传递参数值:
render :partial,
:locals => {
:content => { :value => 'Sample value' }
}
我该如何这样做吗?
I am using Ruby on Rails 3.0.10 and I would like to pass some parameters to the default rendering method. That is, if I have a code like
def show
...
respond_to do |format|
format.html # This, by default, renders the 'show.html.erb' file
end
end
I would like to pass some parameters, maybe like (note: the following doesn't work)
def show
...
respond_to do |format|
# Here I would like to add some local objects that will be available in the 'show.html.erb' template file
format.html { render ..., :locals => { :content => { :value => 'Sample value' } } }
end
end
so that in the show.html.erb
template I can make something like:
<%=
content[:value]
# => 'Sample value'
%>
In few words, I would like to pass parameter values in the same way as made for partial template rendering related to the :locals
key:
render :partial,
:locals => {
:content => { :value => 'Sample value' }
}
How can I do that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您完全可以按照您所描述的进行操作。我在http://apidock.com/rails/ActionController/Base/render标题下查找了它渲染模板”并自己尝试了一下。你每天都会学到新东西。
你可以简单地做
我会考虑为什么你需要这样做而不是使用实例变量。也许有更好的方法。
You can do exactly what you described. I looked it up here http://apidock.com/rails/ActionController/Base/render under the heading "Rendering a template" and gave it a whirl myself. You learn something new everyday.
You can simply do
I would consider why you need to do this instead of using instance variables. Maybe there is a better approach.
设置一个实例变量怎么样?
How about setting an instance variable?
通常,当我们使用操作
show.html.erb
时,它是一个action
视图而不是partial
视图,我们通过以下方式传递参数:控制器上的实例变量,例如现在在
app/views/foos/show.html.erb
文件中,我们可以访问@foo
。渲染部分时,有几种传递参数的方法:
默认情况下,这将渲染部分
app/views/foos/_foo.html.erb
因为它知道@foo
是类型为Foo
。在其中,您将可以自动访问foo
变量。这里我们将渲染
app/views/foos/_foo_details.html.erb
,并传入一个对象。该对象采用部分的名称,因此在_foo_details.html.erb
中,我们可以访问名为foo_details
的变量。最后,与您的问题主要相关的是,我们将渲染一个名为
_foo_things.html.erb
的部分,并向其传递一些局部变量。在这种情况下,您将获得一个名为title
的局部变量,您可以使用它。我希望这能回答你的问题。
Usually when we're working with actions
show.html.erb
is anaction
view rather than apartial
view, we're passing parameters via instance variables on the controller such asNow in the
app/views/foos/show.html.erb
file, we have access to@foo
.When rendering partials, there are a few ways to pass parameters:
This will render partial
app/views/foos/_foo.html.erb
by default because it knows@foo
is of typeFoo
. In it, you will have access to afoo
variable automatically.Here we will render
app/views/foos/_foo_details.html.erb
, and pass in an object. The object takes the name of the partial, so inside_foo_details.html.erb
we'll have access to a variable calledfoo_details
.Finally, and mostly related to your question, we'll render a partial called
_foo_things.html.erb
and pass it some locals. In this case, you'd get a local variable calledtitle
which you could work with.I hope that answers your question.