【问题标题】:Ruby on Rails Form_for resource in other fileRuby on Rails Form_for 其他文件中的资源
【发布时间】:2015-04-09 21:14:39
【问题描述】:

嗯,我对 form_for 有问题,使用 gem 设计。 在我把它放在 users/session/new.html.erb 之前

<h2>Log in</h2>

<%= form_for(resource, as: resource_name, url: session_path(resource_name), html: {class: 'form-horizontal'}) do |f| %>
<div class="form-group">
    <%= f.label :email, class: "col-sm-2 control-label" %>
    <div class="col-sm-6">
        <%= f.email_field :email, autofocus: true , class: "form-control" %>
    </div>
</div>

但现在我尝试在引导模式中使用此代码,在其他文件中,完全在 app/views/layouts/_header.html.erb 中,因为我有一个“登录”按钮。

当我放在那里时 -> 未定义的局部变量或方法 `resource' for

解决方案?或者我需要把这段代码放在哪里?

【问题讨论】:

    标签: ruby-on-rails ruby devise modal-dialog form-for


    【解决方案1】:

    诀窍是在 ApplicationHelper 中定义 resource_nameresourcedevise_mapping 辅助方法。假设您的设计模型名为 user,请将以下方法放入您的应用程序帮助程序中:

    # application_helper.rb
    
    module ApplicationHelper
      def resource_name
        :user
      end
    
      def resource
        @resource ||= User.new
      end
    
      def devise_mapping
        @devise_mapping ||= Devise.mappings[:user]
      end
    end
    

    【讨论】:

    • @user3486836,然后您可以将我的解决方案标记为答案(绿色勾号)。 :)
    猜你喜欢
    • 2011-04-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-29
    • 2012-08-19
    • 1970-01-01
    相关资源
    最近更新 更多