I am having some difficulty figuring out how to route static pages in rails 4. I have created a controller called PagesController and so I also have a views folder called pages with the oakville.html.erb file in it.
My controller looks like this:
class PagesController < ApplicationController
def our_mission
end
end
My routes file looks like this:
get "oakville", :to => "pages#oakville"
I am assuming that I should be able to get to this page by going to localhost:3000/oakville ??
Yes, but you need to add a controller action for oakville
class PagesController < ApplicationController
def oakville
end
end
Also, you will need to create oakville.html.erb and put this into your views/pages directory
The methods in your controller are called actions, and for each static page that you want to be able to navigate to you will need a corresponding controller action. When a person (or link) navigates to yoursite/oakville your routes file needs to know which controller action to perform for the oakville branch of the url.
In the routes that you have shown, get "oakville", :to => "pages#oakville" you are asking the controller to render the oakville action. But there is no oakville action in your controller. Add one and problem solved:
class PagesController < ApplicationController
def our_mission
end
def oakville
end
end
The route you've shown and the action you've shown are completely unrelated.
If you want to route a url like http://www.example.com/oakville to an action called our_mission on the Pages controller, the route looks like this:
get 'oakville' => 'pages#our_mission'
What you've written indicates you expect an action called oakville to exist, and according to the code you've provided, it doesn't.
Related
I'm not exactly sure if I have a clear understanding what rendering means in RESTful routes.
For example:
In my Pages controller,
class PagesController < ApplicationController
def home
render "home.html.erb"
end
end
on my routes.rb file
i have the following:
get "/" => "pages#home"
does render home.html.erb mean output the info on this page?
Thanks!
Yes. render does the work of rendering your application’s content for use by a browser when your action is invoked. You don't really need to explicitly specify the name of the view if your view name matches the action name and placed in the right folder in app/views
for eg, if you have your view in app/views/pages/ , your controller can just be
class PagesController < ApplicationController
def home
end
end
And even if you want to render a template which name is different than the action name (or localized in another place); you don't need to specify the file extension, only its name (path/name if outside the scope of the designated folder for the views of your controller)...
Ex, if you have a template app/views/pages/home_template.html.erb for your home action you could do
class PagesController < ApplicationController
def home
render 'home_template'
end
end
Let's say I have a Rails Website or Application
I have controllers for pages and posts
I want to create on my homepage section where I can see all pages and below that section for the posts
What is the common way to do that with the routes.rb? since I have two controller I don't know how to create the homepage
To make your code more clear you can use separated controller for that. For example create HomepageController with action home and prepare all resources you need there:
class HomepageController < ApplicationController
def home
#pages = Page.where(....)
#posts = Post.where(....)
end
end
Create corresponding view file - views/homepage/home.html.erb
Then at the end of your routes.rb add:
root to: "homepage#home"
I want to redirect some links that point to my public folder in Rails.
For example:
http://site.com/example should redirect to http://site.com/example/index.html
I only want to do this for the content that is my public folder.
I tried this in the config/routes.rb file:
match "*pages" => redirect("/%{pages}/index.html")
But it enters a redirect loop.
So http://site.com/example redirects to to http://site.com/example/index.html which also redirects to http://site.com/example/index.html/index.html and so on.
For your solution, Create a page controller and define your page action
for example
your controller name is PagesController
class PagesController < ApplicationController
def about_us
end
end
create a folder in views, name is pages and create a file that name is about_us. (like - "about_us.html.erb" and write content.
In your route file define
match "/pages/about_us" => "pages#about_us", :as => :about_us"
I noticed that an index view is routed correctly even if there isn't a controller method index.
As an example, the routes.rb has this route
AppName::Application.routes.draw do
get 'about' => "about#index"
end
My controller looks like this with no index method (def index end)
class AboutController < ApplicationController
end
and I have a view called index.html.erb in the views/about folder
What's happening here? Is this a case of rails magic where they automatically show the view even if there is no controller method? I couldn't find any documentation on this...
If you have the view file, it'll go ahead and render that implicitly, as documented here
See also, this SO thread on how Rails renders your view files and controller actions.
I have my regular ApplicationController class & I have a Admin::ApplicationController class. The problem is that Admin::ApplicationController doesn't seem to be getting loaded or executed or anything. Am I not allowed to have a namespaced application controller? The reasoning for wanting to have it is so that I can check if a user is an admin w/ CanCan & redirect them out if they're not.
Call this controller Admin::BaseController, as it is more to act as the base of the namespace than to do anything for the appilcation. For it to do what you want to do, you will need to make all admin namespaced controllers inherit from this controller.
The only times I've seen namespacing like that is when the controller is nested in a subfolder. So Admin::ApplicationController would expect to be in controllers/admin/application_controller.rb
One possible solution:
If you want everything except your home page to kick them out, simply set a before_filter on your application controller with an exception for home/index like this:
ApplicationController.rb
before_filter :authorize_admin
def authorize_admin
//dostuff
end
HomeController.rb
skip_before_filter :authorize_admin, :only => ['index']
Where index is your action that you want to skip. Leave off the only to skip the filter for the whole controller.