【问题标题】:Rails: can't pass variable to partial, what am I doing wrong?Rails:不能将变量传递给部分,我做错了什么?
【发布时间】:2014-08-20 06:38:17
【问题描述】:

我正在尝试几种不同的方法来定义局部变量并将其传递给局部变量,但它一直说它是未定义的:

在显示文件中:

<% @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 %>

这些是我尝试传递变量但仍然未定义的其他方式:

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

【问题讨论】:

    标签: ruby-on-rails ruby ruby-on-rails-4 parameter-passing partials


    【解决方案1】:

    摆脱:partial。您在 Rails 中的多个版本都不需要它。

    将名为 startup 的局部变量传递给局部变量的正确方法是:

    render "profile/startup", startup: startup
    

    【讨论】:

    • 我不断收到这个“undefined local variable or method startup':/?
    • 非常感谢,我的代码中有两行,后来我发现了问题。
    • 非常感谢 :) 我遇到了类似的问题。 Rails 令人困惑。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-01-16
    • 2011-06-09
    • 1970-01-01
    • 2019-03-04
    • 2021-02-23
    • 1970-01-01
    • 2013-09-10
    相关资源
    最近更新 更多