问题
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