本文介绍了Rails:无法将变量传递给部分,我做错了什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试几种不同的方法来定义局部变量并将其传递给局部变量,但它一直说它未定义:
I'm trying a few different ways to define and pass the local variable to the partial, but it keeps saying it's undefined:
在显示文件中:
<% @startups.each do |startup| %>
<%= render :partial => "profile/startup" %>
<% end %>
部分:
<%= simple_form_for [@commentable, @comment], :remote => true do |form| %>
<%= form.input :content, label: false, :input_html => { :id => "#{startup.user_id}" } %>
<%= form.submit "Submit" %>
<% end %>
这些是我尝试传递变量的其他方式,但仍然未定义:
These are the other ways I'm trying to pass the variable, but still getting undefined:
<%= render :partial => "user_comments/uac", object: startup, as: startup %>
<%= render :partial => "user_comments/uac", collection: startup, as: startup %>
<%= render :partial => "user_comments/uac", :locals => {:startup => startup} %>
推荐答案
去掉 :partial
.您在多个版本的 Rails 中都不需要它.
Get rid of :partial
. You haven't needed that in Rails for several versions.
将名为 startup
的局部变量传递给局部变量的正确方法是:
The correct way of passing a local called startup
to a partial is this:
render "profile/startup", startup: startup
这篇关于Rails:无法将变量传递给部分,我做错了什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!