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"
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I'm new to Ruby on rails, and in a month i will start a course on Ruby-on-Rails, but i would like to get some code going before i start the course since i want to learn as much as i can.
I made a project with:
rails new portfolio
Then i did:
rails generate controller portfolio index
To get the front page going.
Rails.application.routes.draw do
get 'portfolio/index'
resources :company
root 'portfolio#index'
end
then:
rails generate controller company
controller:
class CompanyController < ApplicationController
def new
end
end
Then i made a file under views/portfolio/ called index.html.erb where my front page will be.
Under views/company/ i will have a file called company.
When i now go to the url: localhost:3000/company/company i get the error:
The action 'show' could not be found for CompanyController
My CompanyController is this:
class CompanyController < ApplicationController
def new
end
end
Anyone that knows alot about ruby that can just give me a little pointer in the right direction?
Thanks.
Start with a single model and controller for companies. Create a index method inside the app/controllers/companies_controller. Then create the content inside file app/views/companies/index.html.erb to check that everything works, for example:
<h1> Hi! This is root page and index method in CompaniesController! </h1>
In config/routes.rb, you must specify a plural name for companies if you plan to create and process more than one, and leave it as it is, if the entity is the only one company for this project. Set plural name for this resource for this moment to create standard routes for CRUD:
resources :companies
root to: "companies#index"
More about routes you can find in rails guide.
You can try using the built-in scaffold generator in order to quickly generate the application skeleton:
rails generate scaffold companies
The command above will generate controller, model, views and routes with CRUD methods in controller and views for the controller methods. Each view in app/views/"resource_name_plural" adjusted with method in controller in config/routes.rb file. This is how the MVC pattern works.
If you want to create static pages, maybe you should look at the high_voltage gem.
In Rails you need to pay careful attention to pluralization. When declaring routes for a resource it should always be the plural form unless its the rare case where the resource really is singular (there can be only one).
Rails.application.routes.draw do
resources :companies
end
This will route to all companies at /companies and a single company at /companies/:id. If you thus try to get /companies/company it will be routed to the #show action since /company will be interpreted as the id.
Controllers should also be named in plural:
# app/controllers/companies_controller.rb
class CompaniesController < ApplicationController
before_action :set_company, only: [:show, :edit, :update, :destroy]
# GET /companies
def index
#companies = Company.all
end
# GET /companies/:id
def show
end
# ...
private
def set_company
#company = Company.find(params[:id])
end
end
You can use the scaffold command to get a full example of a standard rails CRUD controller:
rails g scaffold companies
If you have the create method, or new, you have to include the show method and index method.
in your routes
resources :companies
resources :portfolios
try in your controller:
class CompanyController < ApplicationController
def new
end
def new
end
def index
#companies = Company.all
end
def show
end
end
and if you have the controller you can create the views and point without problem
localhost:3000/company/company
This, I think it should be something more like this.
localhost:3000/companies/
always plural.
Your controller will look for the view defined in the method. look how it is by default, that of a project of mine. in your place of groups will be companies.
look at the name of the views, are the same names of the methods of your controller. right?
I got link on show like below
I Want http://localhost:3000/admin/flipcart
(flipcart is company name so i want each company separate link.)
If I do http://localhost:3000/admin/flipcart so it show flipcart's show page with out login and authentication.
So how can i do.
You could try something like this inside your routes.rb:
get '/admin/:company', as: :admin_company_index, to: 'admin#index'
Then inside the controller just load the company passed through params before you perform any actions:
class AdminController < ApplicationController
before_action :load_company
private
def load_company
#company = Company.where(["name = :c", {c: params[:company]} ])
end
end
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
So I have this Ruby on Rails course and I have to create a really simple blog application for it. But it's giving me a hard time!
I have this model called 'articles' and I can see a list with all the articles by navigating to /articles. I made a controller for it by following the Rails documentation.
My teacher also wants me to make an 'admin' area, so I need to access the same list by going to /admin/articles.
How can I do that?
I know this is a really dumb question but I couldn't find the answer anywhere.
Thank you for your time!
you need to create routes for /articles and for /admin/articles
routes.rb
get "/articles" => "articles#index"
namespace :admins do
get "/articles" => "articles#index"
end
And create 2 controllers.
1)
class ArticlesController < ApplicationController
def index
#articles = current_user.articles
end
end
and for admin, create another controller under folder admins.
2)
class Admins::ArticlesController < Admins::BaseController
def index
#articles = Articles.all
end
end
Here inherit Basecontroller for better coding and handling controllers with different namespace. Or you can inherit application controller also.
And one more thing, Please create model with singular name like article only, and plural for controller like articles as per rails standard naming convention.
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.