Rails Devise:登录后如何登录页面? [英] Rails Devise: How to access sign up page after signed in?

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

问题描述



首先,我通过默认注册页面添加新用户(例如/用户/ sign_up)



然后,我通过按照



在过滤器之前设计阻止访问new_user_registration_path,除非用户已登录



现在,登录进程后,我尝试打开注册页面,它总是指示我到root_path!如何访问注册页面?

我的roots.rb文件如下:

 示例:: Application.routes.draw do 

devise_for:users,:controllers => {:registrations => 'registration'}

资源:公司

资源:订单

资源:客户

root:to => ; welcome#index

end

谢谢大家! >

解决方案

如果您登录,我可以处理能够创建新用户的方案是通过生成用户控制器,创建将保存到User模型的操作方法。 (这是从目前的应用程序剥离,所以希望我没有错过任何东西)



user_controller.rb

  def new 
@user = User.new
end

def create
@user = User.new(params [:user])

如果@ user.save
flash [:notice] =成功创建用户。
redirect_to root_path
else
render:action => 'new'
end
end

views / user / new .html.erb

 <%= form_for @user,:url => user_index_path do | f | %GT; 
< p><%= f.label:email%>
<%= f.text_field:email%>< / p>

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

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

< p><%= f.submit保存%>< / p>
<%end%>

config / routes.rb(Rails 3)

 资源:user,:controller => user

链接到新用户页

 <%= link_to'新用户',new_user_path%> 


I am new with rails and i am using "devise" gem for authentication purposes.

At first i add a new user through default sign up page (E.g./users/sign_up)

Then, i made "sign_up" page only available to signed_in users by following instructions from

Devise before filter that prevents access to "new_user_registration_path" unless user is signed-in

Now, after sign in process when i try open sign up page it always directs me to root_path! How can i access sign up page?
My "roots.rb" file as follows:

Example::Application.routes.draw do

  devise_for :users, :controllers => { :registrations => 'registrations'}

  resources :companies

  resources :orders

  resources :customers

  root :to => "welcome#index"

end

Thank you all!

解决方案

The way I handled the scenario of being able to create New Users if you are signed in was by generating a User controller and having new and create action methods that would save to the User model. (This was stripped from a current app I'm working on so hopefully I didn't miss anything)

user_controller.rb

def new
  @user = User.new
end

def create
  @user = User.new(params[:user])

  if @user.save
    flash[:notice] = "Successfully created User." 
    redirect_to root_path
  else
    render :action => 'new'
  end
end

views/user/new.html.erb

<%= form_for @user, :url => user_index_path do |f| %>
  <p><%= f.label :email %>
  <%= f.text_field :email %></p>

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

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

  <p><%= f.submit "Save" %></p>
<% end %>

config/routes.rb (Rails 3)

resources :user, :controller => "user"

Link to the New User page

<%= link_to 'New User', new_user_path %>

这篇关于Rails Devise:登录后如何登录页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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