How to create view in RoR if skipped during controller generation - ruby-on-rails

When I run this:
rails generate controller hello index
it no doubt generates hello controller, but accidentally when I run another command like this:
rails generate controller world
it creates the world controller successfully, but missed the Route "world/index" like as "hello/index". For this mistake I need to use destroy controller and then generate it once more, is thr some kind of mid way command that I can generate if forgotten something rather than destroying and creating every time.
This command
rails generate controller contact-us index
creates a route as contact_us/index or contact_us after changing routes.rb under config folder. How could I create a more SEO friendly URL in RoR? Like localhost:3000/contact-us?
I am working on some very basic rules to follow RoR..like 3 static pages (Home, About us, Contact Us) Just simple html content to understand more, will certainly add more features to it as well.
localhost:3000/home
localhost:3000/About_us
localhost:3000/contact_us
I created this via creating home, About_us, contact_us controller command and then changed html in views. Since I am on an initial phase, I read somewhere for static pages we can create this in our public like what we have error pages there in the folder or the approach im using is correct?

when you use rails generator it will create controller and view folder for it
rails generate controller test
will create test_controller.rb and view/test folder
rails generate controller test index
will create test_controller.rb and view/test/index.html.erb file as well as define a route for you
However what its sounds like you are trying to do is have single controller with static pages what i would suggest you do is generate home_controller with home, aboutus, and contact actions and than map the routes to them like so
rails generate controller home
controllers/home.rb
HomeController < ApplicationController
def index
end
def about_us
end
def contact
end
end
routes.rb
match '/home', :to => 'home#index'
match '/About_us', :to => 'home#about_us'
match '/Contact_us' , :to=> 'home#contact_us'
and than define your views in
views/home/index.html.erb
views/home/about_us.html.erb
views/home/contact_us.html.erb

Related

Why am I getting this Ruby on Rails Error?: #index is missing a template for request formats: text/html

I am a beginner to Ruby on Rails and just web app development in general. I am having a lot of trouble trying to simply display another page. Here are my codes.
Routes file:
Rails.application.routes.draw do
root 'pages#home'
get '/recent', controller: 'recent', action: 'index'
end
Controller File:
class RecentController < ApplicationController
def index
end
end
and the "recent" html.erb file (Its directory is app>views>recent>recent.html.erb):
<h2>Recent Recipes!</h2>
Any help would be appreciated. I really tried to understand what is wrong, but I can't seem to figure it out. Thanks!
Usually when you create an action, you must have a view (erb) with the same name, so, in this case you can modify the name of recent.html.erb to index.html.erb
Also you can modify the action in your routes from index to recent, and do the same change in the RecentController.
By default Rails expects a view template in the folder with same name as the controller named after the controller's action. If you rename recent.html.erb to index.html.erb it should be ok

Understanding routing with rails

I am trying to make a stupid simple CMS for one of my clients using rails. I generated scaffolding for Page and have been successful at creating and rendering those pages, however, the routing is ugly.
Right now, if I want to view the home page, I get a url like this: example.com/pages/1
I'd like to accomplish 2 things:
How do I set the routing so that example.com automagically grabs the page named "home"
and
How do I set the routing so that example.com/page/page_name performs a
#page = Page.find_by name: 'page_name'
Q1:
How do I set the routing so that example.com automagically grabs the page named "home"
In `routes.rb:
root :to => '[controller]#[action]'
#'pages#home' for example, if your home page is in `pages_controller`
# and uses the `home` action
Q2:
How do I set the routing so that example.com/page/page_name performs a
#page = Page.find_by name: 'page_name'
match '/page/:name', :to => 'pages#some_name', :as => :some_name
this would generate the following in $rake routes:
some_name /page/:name(.:format) pages#some_name
when you link to (or redirect, or otherwise access) this page, you'd write
link_to "click this link!", some_name_path(#SomeModel.some_name)
To accomplish the first thing you need to open the file routes.rb which is in the config folder and add the following:
root to: "home#index"
I'm assuming you have a controller named home which contains a method called index and this is the one that displays the home page.
If you want to make it the same as example.com/pages then you would have to use
root to: "pages#index"
to make a general rule you need to use
root to: "controller#method"
Don't forget to remove the index.html from the public folder too.
I recommend you make the blog application presented here:
http://guides.rubyonrails.org/getting_started.html
It can help you understand more.
Here's a solution that assumes your controller is named pages_controller instead of page_controller.
Add these routes in config/routes.rb:
root to: 'pages#show', page_name: 'home'
get 'pages/:page_name', to: 'pages#show'
For the controller app/controllers/pages_controller.rb:
class PagesController < ApplicationController
def show
#page = Page.find_by(name: params[:page_name])
end
end
Note: In rails4 the find_by dynamic finders have been deprecated... so if you're app is on 4 you should look into updating them. These docs have further details.
Also, if you're just trying to get static looking urls then I would definitely go with Marian Theisen's suggestion and use friendly_id.

creating multiple web page in rails 3 project

I am fairly new to Ruby and Rails. I am using rails 3. I am creating a project where people can login and post jobs and people can search jobs etc. I have created my users model and jobs model and everything is working just fine. I have started creating the home page layout and have added links to the other pages post work, find work, contact, about. I have a few questions hopefully someone can help please.
When I get into the rails server and check out the project and my home page it shows the login set up from the users model and it also shows the jobs model where someone can post a job. I am trying to figure out how I can make it so the jobs do not get posted on the home page but the find work page and people can search from that page? Do I need to link the jobs model to the find work page something like that?
How do I create the web pages for these other links i have established. I can not figure out how to build the page layout for the post work link i have or the other links I have for people to view the page layout. Any help is appreciated? Thanks again!
What you ask is mostly the basics of Rails. You can render jobs or any other model in any view you want, you just need to "send" these jobs to the views from you controller.
1.- Configure routes:
#config/routes.rb
get "/jobs" match 'jobs#index' # i.e 'controller#action'
2.- Configure model controller:
#app/controllers/jobs_controller.rb
class JobsController < ApplicationController
def index
#jobs = Jobs.last(10)
render 'index'
end
end
3.- Configure view:
#app/views/jobs/index.html.erb
<% #jobs.each do |job| "do something with each #{job}" end %>
This is just a birds sight overview you should check some Rails tutorials.
About generating pages for content that isn't in the db...
I had this issue once and solved it like this:
#config/routes.rb
get "/:view" match 'home#views' # rails g controller home
#app/controllers/views_controller.rb
class HomeController < ApplicationController
def views
render params[:view]
end
end
Now generate a view for each new page, lets say "About", under /app/views/home and you can access each of them via /view_name.

Rails controller and routes configuration for website

I have an application in RAILS, it is composed of a set of API, a basic website, and an admin dashboard.
For the API routing I have no problems, as they belong to a model and a controller and are compliant with the RAILS RESTful pattern (a controller for each model, and a method for each HTTP method).
What I'm not comfortable with is writing routes and controllers for website.
The main website is at / so the default route is root :to => "home#index"and I have
Routes for the website pages which look like
get "home/index"
get "map/index"
get "api/index"
get "cgu/index"
get "legal/index"
Which I think it is not good, as I have a controller per view and I need to define a get for each views.
Now for the dashboard I tried a different approach.
It is at /dashboard, the default route is match "dashboard" => "dashboard#index" and here is few pages as an examples
get "dashboard/index"
get "dashboard/users"
get "dashboard/users_stats"
get "dashboard/routes"
get "dashboard/routes_stats"
get "dashboard/charts"
get "dashboard/financial"
So for the dashboard I have a massive dashboard_controller, which contains a def method for each dashboard pages. IE:
#dashboard/users
def users
#users = User.all
respond_to do |format|
format.html {render :layout => 'dashboard'}
end
end
the controller of the dashboard is at /controller but for views and assets I have put it in /folder/dashboard/
Here is 2 questions:
What is the best way to build the home website and dashboard ? Should I have a controller per page or a global controller where I have a method per pages ? (Which I find very convenient, less code).
How should I organize my routes to avoid to set a get "something/something" for each page ?
Or it is normal with RAILS that I have a route defined for each of my page ? I'm fairly new.
EDIT:
To clarify, the dashboard is built around an existing application with API that follow RESTFul Rails pattern:
resources :users
resources :routes
But the dashboard is not tied to any existing resources, it only do stats about those resources.
If you have custom controller action names, then yes, you'll need to define every route. If you use Restful routes, then you can define them easily as
resources :users
which will automatically create routes for actions: index, show, edit, update, create and destroy.
This might be helpful: http://guides.rubyonrails.org/routing.html
For your dashboard, which probably is bringing together a lot of resources, so they'll probably be custom methods. I'd suggest focusing on building your app by individual resource. Then, once you've defined them all, build your dashboard.
I agree with everything that other people have said here. You should definitely try to be more RESTful and create more routes like this:
resources :users
However, there is usually a controller that is not RESTful (usually called pages or static) that serves pages such as Privacy, About Us, etc, etc. For those routes, I usually do this:
['api', 'privacy', 'us'].each do |p|
get p, :controller => 'pages', :action => p
end
check this guide out if you haven't;
I think you haven't embraced the MVC concept of Rails.
For example, say you have "users". You should have users_controller.rb, users.rb (model), /views/users (view) under the app directory.
users_controller contains the index, show, create, etc default actions and your custom actions like stats
user.rb contains instance/static/helper methods
/views/users/ contains templates that corresponds to actions in the controller.

Rails beginner config/routes.rb issue

I am having a little trouble understanding what config/routes actually routes to. For example lets say I started a brand new project and generated a Users controller and edited my config/routes.rb to look like this:
config/routes.rb
SampleApp::Application.routes.draw do
match '/signup', to: 'users#new'
end
I start the server and as expected, it says I don't have a "new" action in my Users controller. I create one:
users_controller.rb
class UsersController < ApplicationController
def new
end
I refresh the page and as expected it tells me I need a users/new template. So my question is: do my view templates always have to be the same as the controller and action names in "(controller name)/(action name)" format (in this case users/new.html.erb)? Is it not possible to name my template something random (e.g. users/rubyonrailsmeetup.html.erb instead of users/new.html.erb) if have a controller action linked to one of the site's functions?
Also, does adding "resources :users" to config/routes.rb by default match the view template file names with the behavior I mentioned above so that views must be named after their "controller/action" names?
Sorry for the bother, I'm just trying to figure out what's part of Rails' magic and what isn't.
Rails attempts to render the template with the same name as the action by default, if no other render or redirect is invoked in the controller action. Basically, there's an implicit render :action at the end of every controller action.
But you can override this easily enough by adding an explicit render, e.g.,
render :rubyonrailsmeetup
Edit for clarity: this call to render goes in the controller code, not in config/routes
do my view templates always have to be the same as the controller and action names in "(controller name)/(action name)" format
These are defaults, you can render any view from the action by giving render :view_file_rel_path at the end of the action
does adding "resources :users" to config/routes.rb by default match the view template file names
Anything added in routes.rb is directly related upto controllers only, i.e. it matches the request and maps it to the controller/action. Logic of view comes only inside the action code

Resources