如何向默认渲染方法传递一些参数?

发布于 2024-12-03 04:19:13 字数 906 浏览 2 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

逆夏时光 2024-12-10 04:19:13

您完全可以按照您所描述的进行操作。我在http://apidock.com/rails/ActionController/Base/render标题下查找了它渲染模板”并自己尝试了一下。你每天都会学到新东西。

你可以简单地做

def show
  respond_to do |format|
    format.html { render :locals => { :content => { :value => 'Sample value' } } }
  end
end

我会考虑为什么你需要这样做而不是使用实例变量。也许有更好的方法。

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

def show
  respond_to do |format|
    format.html { render :locals => { :content => { :value => 'Sample value' } } }
  end
end

I would consider why you need to do this instead of using instance variables. Maybe there is a better approach.

月下伊人醉 2024-12-10 04:19:13

设置一个实例变量怎么样?

# your_controller.rb

def show
  @content_value = ...
end

 

# show.html.erb

<%= @content_value %>

How about setting an instance variable?

# your_controller.rb

def show
  @content_value = ...
end

 

# show.html.erb

<%= @content_value %>
萌面超妹 2024-12-10 04:19:13

通常,当我们使用操作 show.html.erb 时,它是一个 action 视图而不是 partial 视图,我们通过以下方式传递参数:控制器上的实例变量,例如

def show
  ...
  @foo = "bar"
  respond_to do |format|
    format.html
   end
end

现在在 app/views/foos/show.html.erb 文件中,我们可以访问 @foo

渲染部分时,有几种传递参数的方法:

默认情况下,这将渲染部分 app/views/foos/_foo.html.erb 因为它知道 @foo 是类型为 Foo。在其中,您将可以自动访问 foo 变量。

<%= render @foo %>

这里我们将渲染app/views/foos/_foo_details.html.erb,并传入一个对象。该对象采用部分的名称,因此在 _foo_details.html.erb 中,我们可以访问名为 foo_details 的变量。

<%= render :partial => "foo_details", :object => @foo %>

最后,与您的问题主要相关的是,我们将渲染一个名为 _foo_things.html.erb 的部分,并向其传递一些局部变量。在这种情况下,您将获得一个名为 title 的局部变量,您可以使用它。

<%= render :partial => "foo_things", :locals => {:title=> "Test 123"} %>

我希望这能回答你的问题。

Usually when we're working with actions show.html.erb is an action view rather than a partial view, we're passing parameters via instance variables on the controller such as

def show
  ...
  @foo = "bar"
  respond_to do |format|
    format.html
   end
end

Now 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 type Foo. In it, you will have access to a foo variable automatically.

<%= render @foo %>

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 called foo_details.

<%= render :partial => "foo_details", :object => @foo %>

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 called title which you could work with.

<%= render :partial => "foo_things", :locals => {:title=> "Test 123"} %>

I hope that answers your question.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文