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:
- 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?
- 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".
#r开发者_Go百科outes.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
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.
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
.
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, ...).
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
精彩评论