如何合并在Rails 3中使用设计同时登录和注册页面?并在、页面、Rails

2023-09-10 18:21:05 作者:空心者

因此​​,这是我的设计/会话/ new.html.erb 是这样的:

So this is what my devise/sessions/new.html.erb looks like:

<div id="sign_in">

<%= form_for(resource, :as => resource_name, :url => session_path(resource_name)) do |f| %>
            <%= f.text_field :f_name, :value => "First Name", :class => "clearField curved" %><div class="error"></div><br />
            <%= f.text_field :l_name, :value => "Last Name", :class => "clearField curved" %><div class="error"></div><br />
            <%= f.text_field :username, :value => "Username", :class => "clearField curved" %><div class="error"></div><br />
            <%= f.password_field :password, :value => "Password", :class => "clearField curved" %><div class="error"></div><br />
            <%= f.password_field :password_confirmation, :value => "Password", :class => "clearField curved" %><div class="error"></div><br />
            <%= f.text_field :email, :value => "Email Address", :class => "clearField curved" %><div class="error"></div><br />

        <div id="login_buttons">
            <%= f.submit "Sign in", :id => "login", :value => "Submit", :class => "curved" %>
            <%= f.submit "Sign in", :id => "register", :value => "Register", :class => "curved" %>
            <%= f.submit "Send Reset Instructions", :id => "pass-reset", :value => "Send Reset Instructions", :class => "curved"%>
            <a href="#" id="forgot-pw">Forgot pass?</a>
        </div>

    <% end %>

</div>

以上仅适用于登录(因为:URL =&GT; session_path(RESOURCE_NAME),而不是 registration_path )。

了这个页面,现在呢,是在页面加载它隐藏除了用户名和放大器各个领域;密码字段(即默认为登录页)。然后,当他们preSS的注册按钮,就消失在了别人。

What this page does now, is on pageload it hides every field except the username & password field (i.e. it defaults to the login page). Then when they press the 'Register' button, it fades in the others.

不过,当你preSS提交不起作用 - 因为错误的表单处理程序是管理它

However, when you press submit that doesn't work - because the wrong form handler is managing it.

这是我的正常登记表的样子(它的工作原理,顺便说一句)在设计\注册\ new.html.erb

This is what my regular registration form looks like (which works, btw) at devise\registrations\new.html.erb:

<h2>Sign up</h2>

<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
  <%= devise_error_messages! %>

  <p><%= f.label :username %><br />
  <%= f.text_field :username %></p>

  <p><%= f.label :email %><br />
  <%= f.text_field :email %></p>

  <p><%= f.label :password %><br />
  <%= f.password_field :password %></p>

  <p><%= f.label :password_confirmation %><br />
  <%= f.password_field :password_confirmation %></p>

  <p><%= f.submit "Sign up" %></p>
<% end %>

<%= render :partial => "devise/shared/links" %>

我的路线文件的适用部分看起来是这样的:

The applicable part of my routes file looks like this:

devise_for :users, :path_names => { :sign_up => "register", 
                                  :sign_in => "login", 
                                  :sign_out => "logout" }

devise_scope :user do
    get "login", :to => "devise/sessions#new"
    get "register", :to => "devise/registrations#new"
    get "logout",   :to => "devise/sessions#destroy"

所以,我想要的行为如下:

So the behavior I want is as follows:

用户去登录,他们看到的只有两个表单域(用户名+密码)。他们preSS输入登录他们进来。

The user goes to login, they see only two form fields (username + password). They press enter it logs them in.

如果他们preSS注册,没有做一个页面加载,我想正确的形式(与所需的额外表单字段:姓,名等)出现,当他们preSS进入它的注册。我也想网址从 myapp.com/login 更改为 myapp.com/register - 无需页面加载。如果pressed'登录'没有填写表格,就应该带他们回到登录页面( myapp.com/login ),只有两个领域(用户名+通)展示。

If they press 'Register', without doing a pageload, I would like the right form (with the additional form fields required: first name, last name, etc.) to appear and when they press enter it does the registration. I would also like the URL to change from myapp.com/login to myapp.com/register - without a page load. If they pressed 'Sign In' without filling out the form, it should take them back to the login page (myapp.com/login) with only the two fields (username + pass) showing.

当我直接链接到 myapp.com/register 它会直接转到一个页面与正确的表单字段和功能是否正常的方式。

That way when I link directly to myapp.com/register it goes directly to that one page with the correct form fields and it functions properly.

基本功能类似于GitHub的方式现在管理通过回购浏览,没有页面刷新(但URL发生变化)。

Basically functionality similar to the way github now manages browsing through a repo with no page refreshes (but the URL changes).

建议?

推荐答案

要在同一页上结合用户注册用户登录,我做了以下内容: 1)将所有的code,从意见/会话/新眼帘/注册/新 2)修改提交按钮ID / JS命名空间:

To combine user registration with user login on the same page, I did the following: 1) Copy all the code from views/sessions/new INTO view/registrations/new 2) Modify the submit button id/js namespace:

<%= f.submit 'submit_button', :style => "display: none", :id => "submitSignInForm" %><br>
<a class="button" href="javascript:document.getElementById('submitSignInForm').click();"> Sign in </a>"

3)覆盖的设计会话控制器的新方法重定向。 创建控制器/ sessions_controller.rb并且加上下面code。请注意,从注册控制器类继承,而不是会话!_爱

3) Override the Devise Session Controller new method with a redirect. create controllers/sessions_controller.rb and insert the following code. Note the controller class inherits from Registrations, not Sessions!

class SessionsController < Devise::RegistrationsController  
  def new
    redirect_to new_user_registration_path
  end
end

*我要指出,这个曾在铁轨2.3.5,但我认为它会在3.0不变。

*I should note, this worked in rails 2.3.5, but I think it will work in 3.0 the same.

 
精彩推荐