【问题标题】:Trying to change content_for into a partial and getting "undefined local variable or method resource ..."试图将 content_for 更改为部分并获取“未定义的局部变量或方法资源......”
【发布时间】:2011-09-22 11:35:33
【问题描述】:

我目前的设计登录表单是这样设置的。而不是 content_for,我只想将此内容放在部分(名为 views/devise/sessions/_new.html.erb 而不是 views/devise/sessions/new.html.erb)中以在布局中呈现。但是当我这样做时,我得到了

undefined local variable or method resource for #<#<Class:0x0000010300f6f0>:0x0000010300da30>

<% content_for :header do %>
    <div id="login">
        <%= form_for(resource, :as => resource_name, :url => session_path(resource_name)) do |f| %>
          <div class="login_header"><%= f.email_field :email, :placeholder => "Email" %></div>

          <div class="login_header"><%= f.password_field :password, :placeholder => "Password" %></div>

          <% if devise_mapping.rememberable? -%>
            <div class="login_header"><%= f.check_box :remember_me %> <%= f.label :Stay_logged_in %></div>
          <% end -%>

          <div class="login_header"><%= f.submit "Sign in" %></div>
        <% end %>
        <br /><br />
        <%= render :partial => "devise/shared/links" %>
    </div>
<% end %>

【问题讨论】:

    标签: ruby-on-rails ruby partial content-for


    【解决方案1】:

    它抱怨它不知道您传递给form_for 的局部变量。您需要传递您的局部变量,称为资源或将其设置为控制器中的实例变量。

    【讨论】:

    • 如何将其设置为实例变量?
    • 在您的控制器操作中,使用@resource = whatever it should be,然后在视图中将其更改为form_for(@resource...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-31
    • 2017-01-26
    相关资源
    最近更新 更多