Rails: can't pass variable to partial, what am I doing wrong?

一笑奈何 提交于 2020-02-02 16:07:16

问题


I'm trying a few different ways to define and pass the local variable to the partial, but it keeps saying it's undefined:

in Show file:

<% @startups.each do |startup| %>
  <%= render :partial => "profile/startup" %>
<% end %>

in partial:

<%= 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} %>

回答1:


Get rid of :partial. You haven't needed that in Rails for several versions.

The correct way of passing a local called startup to a partial is this:

render "profile/startup", startup: startup


来源:https://stackoverflow.com/questions/24480841/rails-cant-pass-variable-to-partial-what-am-i-doing-wrong

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!