如何解决route.rb中的动态根目录授予自己的用户#show
| 我有一个登录部分,一旦有人成功登录,我希望它返回其个人资料用户#show。您将如何使routes.rb做到这一点?解决方法
我假设您有一些可以处理登录并管理会话的内容。
假设您有一个会话控制器,用于登录创建新会话。 (登录表单会创建一个新会话,如果用户名和密码组合正确,则此操作会将帖子发送到该会话的create方法
redirect_back_or user
你的routes.rb有
resources :users
这是会话控制器的示例
SessionsController类def create user = User.authenticate(params[:session][:name],params[:session][:password]) if user.nil? flash.now[:error] = \"Invalid email/password combination.\" @title = \"Sign in\" render \'new\' else sign_in user redirect_to user end end def destroy sign_out redirect_to root_path end end, 您不需要在routes.rb中执行此操作。只需在控制器中对该行为进行编程即可。 在您的登录控制器内设置一个
redirect_to show_user_path(@user)
如果要配置路由,使\'/ \'为\'/ user / show \',则:
root :to=>\'user#show\'
, 我不知道我是否完全理解了这个问题,但我的第一个念头是我不会在route.rb中进行管理。我假设您至少要:
[controller]/[some login type action]
users/show
作为可行的行动。除此之外,我将使用重定向功能从成功的登录名移至“用户/显示”操作。
您可能会看到有关重定向的链接以及有关布局和渲染的链接,以获取更多信息...
, 我想到了。在我的routes.rb中,我有
match \'/account/:id\',:to => \'users#show\',:as => \'account\'
在我的用户会话控制器中,如果他们成功登录,则它将创建@current_user_session。然后创建@current_user。然后,我将成功登录路由到routes.rb中定义的account_path(@current_user)
def create
@user_session = UserSession.new(params[:user_session])
respond_to do |format|
if @user_session.save
@current_user_session = UserSession.find
@current_user = current_user_session && current_user_session.record
format.html { redirect_to(account_path(@current_user),:notice => \'Successfully logged in.\') }
format.xml { render :xml => @user_session,:status => :created,:location => @user_session }
else
format.html { render :action => \"new\" }
format.xml { render :xml => @user_session.errors,:status => :unprocessable_entity }
end
end
end
此时创建@current_user是否有任何缺陷?或者,
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。