如何将登录表单放在 Devise 和 Rails 的标题中 [英] How to put a login form in the header for Devise and Rails

查看:26
本文介绍了如何将登录表单放在 Devise 和 Rails 的标题中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的网站的标题中放置一个登录表单.我使用 Rails 3.2.8 和最新的 Devise.在 app/views/devise/sessions/new.html.erb 文件中创建一个登录表单:

 <%= form_for(resource, :as => resource_name, :url => session_path(resource_name), :html => {:class => 'main'}) do |f|%><div><%= f.label :login %><%= f.text_field :login, :placeholder =>'输入用户名'%>

<div><%= f.label :password %><%= f.password_field :password, :placeholder =>'输入密码'%></div><% if devise_mapping.rememberable?-%><div><%= f.check_box :remember_me %><%= f.label :remember_me %></div><%结束-%><%= f.submit "Go" %><%结束%>

如果我尝试复制该代码并将其放入我的 application.html.erb,我会收到有关上面引用的资源变量的错误:

未定义的局部变量或方法`resource' for #<#:0x3c73b00>

那么我可以使用带有 /users/sign_in 的普通 Rails 表单作为操作吗?登录后如何告诉设计重定向到哪里?

解决方案

我们在 Devise wiki 上有一个 wiki 页面:

https://github.com/plataformatec/devise/wiki/How-To:-Display-a-custom-sign_in-form-anywhere-in-your-app

享受吧!

I want to put a login form in the header of my website. I use Rails 3.2.8 and the latest Devise. In the app/views/devise/sessions/new.html.erb file a login form is created with:

  <%= form_for(resource, :as => resource_name, :url => session_path(resource_name), :html => {:class => 'main'}) do |f| %>
    <div><%= f.label :login %> <%= f.text_field :login, :placeholder => 'Enter username' %></div>
    <div><%= f.label :password %> <%= f.password_field :password, :placeholder => 'Enter password' %></div>
    <% if devise_mapping.rememberable? -%>
      <div><%= f.check_box :remember_me %> <%= f.label :remember_me %></div>
    <% end -%>
    <%= f.submit "Go" %>
  <% end %>

If I try to copy that code and put it in my application.html.erb, I get an error regarding that resource variable referenced above:

undefined local variable or method `resource' for #<#<Class:0x3cb62c8>:0x3c73b00>

So can I just use a normal rails form with /users/sign_in as the action? How do I tell devise where to redirect to after logging in?

解决方案

We have a wiki page for that on the Devise wiki:

https://github.com/plataformatec/devise/wiki/How-To:-Display-a-custom-sign_in-form-anywhere-in-your-app

Enjoy!

这篇关于如何将登录表单放在 Devise 和 Rails 的标题中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆