简体   繁体   English

Rails中用于到达页面的动态路由

[英]Dynamic Routing in Rails for landing page

I currently have this in my routes.rb 我的routes.rb中目前有这个

get 'admin', to: 'admin/appointments#index'

I want the route to be dynamic based on the user that logs in. But I can't do it like this, where should I set this? 我希望路由基于登录的用户是动态的。但是我不能这样做,应该在哪里设置呢?

get 'admin', to: @current_admin.landing_page

It sounds like you're trying to introduce Controller logic into your routes. 听起来您正在尝试将Controller逻辑引入到路由中。 You probably want one admin_landing_page route, to which you supply current user as a parameter. 您可能需要一个admin_landing_page路由,您将当前用户作为参数提供给该路由。

So in your Routes, you want something like this 所以在您的路线中,您想要这样的东西

get 'admin', to: landing_page/:admin_identifier

And in your controller 而在您的控制器中

@current_admin = YourAdminModel.find(params:[:admin_identifer])

Then you can use @current_admin in your view to customize it based on data from the current_admin, such as 然后,您可以在视图中使用@current_admin基于current_admin中的数据对其进行自定义,例如

<h1>Welcome to the homepage of Admin <%= @current_admin.name %>!</h1>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM