render in routes RESTful routes - ruby-on-rails

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

Related

How to create nested files in welcome view rails

I have 2 folders with some views that I want to display on the welcome/main index of my rails app.
Views
Main
Owner
info.html
I can route to the file but the controller for main has no direction on how to get there. I tried
class MainController < ApplicationController
def owner
def info
end
end
But I know this isn't right. What do I need to do?
There are 2 ways you can handle this,
Using namespaces,
# app/controllers/owner/main_controller.rb
module Owner
class MainController < ApplicationController
def info
end
end
end
# app/views/owner/main/info.html
<html>...</html>
Note the change in file structure of view
or with explicit render with name of view
class MainController < ApplicationController
def info
render 'main/owner/info' # Relative path from app/views
end
end

How does rails homepage works?

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"

Routing Static Pages in Rails

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.

Match url and redirect to url+/index.html in rails

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"

How to execute a controller/action in Rails?

Is it possible to execute an action from within another action?
I'm looking for something in between of 'redirect' and 'render'. I don't want to tell the browser to make an extra request with 'redirect', and 'render' will only execute the view, not the action.
note: I'm using rails 3
#ben-holland Take a look at this http://symfony.com/doc/current/book/controller.html#forwarding
Put the action in a lib/ file and include it in both controllers
#lib/foo_controller_includes.rb
module FooControllerIncludes
def special_edit
do_some_stuff!
render :action=>"/full/path/to/file"
end
end
#app/controllers/bar_controller.b
class BarController < ApplicationController
include FooControllerIncludes
def edit
special_edit
end
end

Resources