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

查看:99
本文介绍了如何将登录表单放在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 | %GT; 
< div><%= f.label:login%> <%= f.text_field:login,:placeholder => '输入用户名'%>< / div>
< div><%= f.label:password%> <%= f.password_field:password,:placeholder => '输入密码'%>< / div>
<%devise_mapping.rememberable? - %GT;
< div><%= f.check_box:remember_me%> <%= f.label:remember_me%>< / div>
<%end - %>
<%= f.submitGo%>
<%end%>

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

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

所以我可以使用正常的rails表单与 / users / sign_in 作为动作?登录后,如何告诉设计重定向到哪里?

解决方案

我们在Devise 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天全站免登陆