Ruby-on-rails Rails 4 - 将变量传递给部分
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16242121/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Rails 4 - passing variable to partial
提问by dopplesoldner
I am following the Ruby on Rails tutorial and have come across a problem while trying to pass variables to partials.
我正在关注 Ruby on Rails 教程,并在尝试将变量传递给部分时遇到了问题。
My _userpartial is as follows
我的_user部分如下
<li>
<%= gravatar_for user, size: 52 %>
<%= link_to user.name, user %>
</li>
I would like to pass in a number for the size value. I am trying as follows without any luck.
我想为大小值传递一个数字。我正在尝试如下但没有任何运气。
<%= render @users, :locals => {:size => 30} %>
回答by Karim Sonbol
From the Rails api on PartialRender:
来自PartialRender上的 Rails api :
Rendering the default case
If you're not going to be using any of the options like collections or layouts, you can also use the short-hand defaults of render to render partials.
渲染默认情况
如果您不打算使用任何选项,如集合或布局,您还可以使用渲染的速记默认值来渲染部分。
Examples:
例子:
# Instead of <%= render partial: "account" %>
<%= render "account" %>
# Instead of <%= render partial: "account", locals: { account: @buyer } %>
<%= render "account", account: @buyer %>
# @account.to_partial_path returns 'accounts/account', so it can be used to replace:
# <%= render partial: "accounts/account", locals: { account: @account} %>
<%= render @account %>
# @posts is an array of Post instances, so every post record returns 'posts/post' on `to_partial_path`,
# that's why we can replace:
# <%= render partial: "posts/post", collection: @posts %>
<%= render @posts %>
So, you can use pass a local variable sizeto render as follows:
因此,您可以使用传递局部变量size来呈现如下:
<%= render @users, size: 50 %>
and then use it in the _user.html.erbpartial:
然后在_user.html.erb部分中使用它:
<li>
<%= gravatar_for user, size: size %>
<%= link_to user.name, user %>
</li>
Note that size: sizeis equivalent to :size => size.
请注意,size: size相当于:size => size.
回答by jamesc
You need the full render partial syntax if you are passing locals
如果您传递当地人,则需要完整的渲染部分语法
<%= render @users, :locals => {:size => 30} %>
Becomes
成为
<%= render :partial => 'users', :collection => @users, :locals => {:size => 30} %>
Or to use the new hash syntax
或者使用新的哈希语法
<%= render partial: 'users', collection: @users, locals: {size: 30} %>
Which I think is much more readable
我认为这更具可读性
回答by Mirror318
Either
任何一个
render partial: 'user', locals: {size: 30}
Or
或者
render 'user', size: 30
To use locals, you need partial. Without the partialargument, you can just list variables directly (not within locals)
要使用locals,您需要partial. 没有partial参数,您可以直接列出变量(不在 内locals)
回答by Joshua Pinter
Don't use localsin Rails 4.2+
不要locals在 Rails 4.2+ 中使用
In Rails 4.2 I had to remove the localspart and just use size: 30instead. Otherwise, it wouldn't pass the local variable correctly.
在 Rails 4.2 中,我不得不移除该locals部分并size: 30改为使用。否则,它不会正确传递局部变量。
For example, use this:
例如,使用这个:
<%= render @users, size: 30 %>
回答by Apoorv
If you are using JavaScript to render then use escape_JavaScript("<%=render partial: partial_name, locals=>{@newval=>@oldval}%>");
如果您使用 JavaScript 进行渲染,请使用 escape_JavaScript("<%=render partial: partial_name, locals=>{@newval=>@oldval}%>");
回答by Alexander Luna
Syntactically a little different but it looks cleaner in my opinion:
语法上有点不同,但在我看来它看起来更干净:
render 'my_partial', locals: { title: "My awesome title" }
# not a big fan of the arrow key syntax
render 'my_partial', :locals => { :title => "My awesome title" }
回答by Carlos
ou are able to create local variables once you call the render function on a partial, therefore if you want to customize a partial you can for example render the partial _form.html.erbby:
一旦您在部分上调用渲染函数,您就可以创建局部变量,因此,如果您想自定义部分,您可以例如_form.html.erb通过以下方式渲染部分:
<%= render 'form', button_label: "Create New Event", url: new_event_url %>
<%= render 'form', button_label: "Update Event", url: edit_event_url %>
this way you can access in the partial to the label for the button and the URL, those are different if you try to create or update a record.
finally, for accessing to this local variables you have to put in your code local_assigns[:button_label](local_assigns[:name_of_your_variable])
通过这种方式,您可以访问按钮和 URL 标签的部分内容,如果您尝试创建或更新记录,则它们会有所不同。最后,为了访问这个局部变量,你必须放入你的代码local_assigns[:button_label]( local_assigns[:name_of_your_variable])
<%=form_for(@event, url: local_assigns[:url]) do |f| %>
<%= render 'shared/error_messages_events' %>
<%= f.label :title ,"Title"%>
<%= f.text_field :title, class: 'form-control'%>
<%=f.label :date, "Date"%>
<%=f.date_field :date, class: 'form-control' %>
<%=f.label :description, "Description"%>
<%=f.text_area :description, class: 'form-control' %>
<%= f.submit local_assigns[:button_label], class:"btn btn-primary"%>
<%end%>

