ruby-on-rails – 如何将一些参数传递给默认的渲染方法?

前端之家收集整理的这篇文章主要介绍了ruby-on-rails – 如何将一些参数传递给默认的渲染方法?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用 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' }
       }

我怎样才能做到这一点?

@H_301_18@

解决方法

你可以完全按照你的描述做.我在 http://apidock.com/rails/ActionController/Base/render标题“渲染模板”下查找了它并自己给了它一个旋转.你每天学习新的东西.

你可以干脆做

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

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

@H_301_18@ @H_301_18@ 原文链接:https://www.f2er.com/ruby/265196.html

猜你在找的Ruby相关文章