Rails 应用程序结构

发布于 2024-10-31 14:13:03 字数 1141 浏览 0 评论 0原文

我正在开发 Rails 应用程序,对 MVC 和 REST 有点陌生。我正在尝试使用这些方法,但有几个问题:

  1. 我需要有不直接对应于“资源”的导航链接。假设它们是“仪表板”、“交易”和“余额”。我如何在控制器中对此进行建模?我是否只有一个具有三个操作的“导航”控制器,每个操作对应上述每个单词?
  2. 如果这是对应用程序进行建模的合理方法,我想我会制作 /dashboard => 的匹配路由Navigation#Dashboard,因为我不想在 URL 中暴露“Navigation”一词。这看起来不错,但是当我在服务器中渲染“仪表板”时,URL 会显示“导航/仪表板”,而不仅仅是“仪表板”,因为我直接调用了它。我发现的唯一解决方法是重定向到“/dashboard”,但这似乎是错误的。

谢谢!

编辑: 真正的问题是,当有人进入“登录”并且出现验证错误时,我想显示该错误,并呈现sessions#new(这是登录页面)视图。这会导致浏览器转到 /sessions/new,但我希望它转到 /signin。我想避免使用redirect_to,因为这会导致浏览器执行整个第二页请求,这似乎不“正确”。

#routes.rb  
match '/signin', :to => "sessions#new", :as => :signin

#sessions_controller.rb  

def create
    @title = "Sign In"
    user = User.authenticate(params[:session][:email], params[:session][:password])

    if user.nil?
        flash[:error] = "Invalid email/password combination."
        #I want to use render 'new', but that changes the URI in the browser to sessions/new, whereas I want to maintain /signin without redirecting

        redirect_to signin_path
    else
        sign_in user
        redirect_to dashboard_path
    end    
end

I am working on a Rails application and am sort of new to MVC and REST. I am trying to use these methodologies but have a couple of questions:

  1. I need to have navigation links that don't correspond directly to a "resource." Let's say they are "Dashboard" "Transactions" and "Balances." How do I model this in a controller? Do I just have a controller for "Navigation" that has three actions, one for each of the words above?
  2. If this is a reasonable way to model the application, I figured I would make match routes that say /dashboard => Navigation#Dashboard, since I wouldn't want to expose the word "Navigation" in the URL. This seems good, but then in the server when I do a render 'dashboard' the URL says 'navigation/dashboard' instead of just 'dashboard' since I called it directly. The only work around I have found is to redirect to '/dashboard', but that seems wrong.

Thanks!

Edit:
The real question is that when someone goes to "Sign In" and there is a validation error, I want to show the error, and render the sessions#new (which is the sign in page) view. This causes the browser to go to /sessions/new, but I want it to go to /signin. I want to avoid using a redirect_to since this will cause the the browser to do a whole second page request which doesn't seem "correct".

#routes.rb  
match '/signin', :to => "sessions#new", :as => :signin

#sessions_controller.rb  

def create
    @title = "Sign In"
    user = User.authenticate(params[:session][:email], params[:session][:password])

    if user.nil?
        flash[:error] = "Invalid email/password combination."
        #I want to use render 'new', but that changes the URI in the browser to sessions/new, whereas I want to maintain /signin without redirecting

        redirect_to signin_path
    else
        sign_in user
        redirect_to dashboard_path
    end    
end

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

和影子一齐双人舞 2024-11-07 14:13:03

对于问题的第一部分,我经常发现自己所处的位置是网站的“实用”功能无法很好地映射到资源上。通常,我将它们固定在某种实用控制器中。您可以尝试 DashboardController 并定义方法 navigationbalancestransactions 等。

对于第二部分,您是否尝试过:

match '/dashboard' => 'navigation#dashboard'

这应该适用于您所描述的内容。如果您还没有阅读过,您应该阅读有关路由的 Rails 指南: http://guides.rubyonrails.org /routing.html

For the first part of the question, I often find myself in a position where there are "utility" functions of the site that don't map well onto resources. Typically, I stick them in some kind of utility controller. You might try DashboardController and define the methods navigation, balances, transactions and so on.

For the second part, did you try:

match '/dashboard' => 'navigation#dashboard'

This should work for what you describe. If you haven't already, you should read the Rails Guide on routing: http://guides.rubyonrails.org/routing.html.

只想待在家 2024-11-07 14:13:03

使用导航控制器就可以了。在您的路线中,您可以为其定义自定义名称。

#routes.rb

match "/dashboard" => "navigation#dashboard", :as => "dashboard"

您将通过定义 :as => 获得 dashboard_path “dashboard”

更新

您无法渲染dashboard_path。 还必须指定文件夹 render 'navigation/dashboard'

如果您在 NavigationController 中,则可以 render 'dashboard',否则您 您的视图中应该有一个名为 navigation 的文件夹,其中包含一个名为 dashboard 的模板。

在您的 NavigationController 中:

class NavigationController < ApplicationController
  def dashboard
    ...
  end
end

要使用链接:<%= link_to 'Dashboard',dashboard_path %>

更新 2

我没有看到您为 sign_in 创建的表单。我假设您正在使用设备。如果是这种情况,请确保您的登录表单发布到 signin_path

It's fine to use a nav controller. In your routes you can define custom names for it.

#routes.rb

match "/dashboard" => "navigation#dashboard", :as => "dashboard"

You will get dashboard_path by defining the :as => "dashboard"

Update

You can't do render dashboard_path. You can do render 'dashboard' if you are in NavigationController, otherwise you have to specify the folder as well render 'navigation/dashboard'

You should in your views a folder called navigation with a template called dashboard.

In your NavigationController:

class NavigationController < ApplicationController
  def dashboard
    ...
  end
end

To use the link : <%= link_to 'Dashboard', dashboard_path %>

Update 2

I don't see the form that you created for sign_in. I assume that you are using devise. If that's the case then make sure that your login form posts to signin_path.

夜唯美灬不弃 2024-11-07 14:13:03

当我实现仪表板时,我使用 DashboardController,而 index 只是仪表板。我通常还会定义一个 HomeController,主页所在的位置(home#index,还有关于页面、反馈页面,...)。

when I implement a dashboard, I use a DashboardController, and the index is just the dashboard. I usually also define a HomeController, where the homepage lives (home#index, and also the about page, the feedback page, ...).

翻身的咸鱼 2024-11-07 14:13:03

1 - 我不太清楚你在问什么。但如果我正确地理解了你的问题,应该有 3 个资源(控制器)用于

1 - 仪表板

2 - 交易

3 - 余额

,并且控制器可能只显示操作

2 - 为此,你可以使用命名路由 (http://guides.rubyonrails.org/routing.html)
例如:匹配“导航”=> 'Dashboard#show'

希望这有帮助

1 - I'm not quit clear what you are asking. But if I get your question correctly, there should be 3 resources (controllers) for

1 - Dashboard

2 - Transactions

3 - Balances

and there controllers might have only show action

2- for that you can use named route (http://guides.rubyonrails.org/routing.html)
Ex: match 'Navigation' => 'Dashboard#show'

hope this helps

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文