Does the application controller have an associated template? - ruby-on-rails

Suppose I run
rails new proj1
I can do
rails generate controller abc def
and that creates among other things, .\app\views\abc\def.html.erb
And I can edit config.rb to say
get '/', to: 'abc#def'
or
root 'abc#def'
And that will load that def.html.erb template when I go to http://127.0.0.1:3000/
But I'm interested if I can do that without creating that new controller. I'm interested in whether I can go to the template just using the application controller.
So, for example, I can edit .\app\controllers\application_controller.rb and add
def a
end
then does the application controller behave like other controllers and try to render a corresponding .html.erb file e.g. the 'a' action would try to render a.html.erb? If so, I can't find where I should place the a.html.erb file?
Other controllers have a subdirectory for them within views, and files within that subdirectory for each action, and one can do them manually or by using e.g. rails generate controller blah a b c to generate the controller with actions and with a template for each action.
But I can't see that for the application controller.. I can't see where its views are.

You can do it like this:
routes.rb:
get '/', to: "application#root"
application_controller.rb
def root
end
app/views/application/root.html.erb
hello world
Running the server and visiting localhost:3000 prints "hello world"
Sinatra does things similar to this. Routes and controller actions are basically combined and it'd be up to you to implement rails-like controllers yourself if you wanted them.

Related

No Route Matches for a very basic Rails4 App

I'm very new to RoR and I'm trying to get a very basic site going. I have my home page working okay, but when I try to add a new page, I keep getting "No route matches" in the log. Here is the output from rake routes:
Prefix Verb URI Pattern Controller#Action
inventing_index GET /inventing/index(.:format) inventing#index
ideas_index GET /ideas/index(.:format) ideas#index
root GET / ideas#index
However, when I go to mysite.com/inventing or mysite.com/inventing/index I get the no route matches error. mysite.com/ shows the app/views/ideas.erb as hoped. All I did was rails generate controller inventing index. Is there something else I have to do to activate the route?
I'm running ruby 2.0.0p247 and rails 4.0.0 with passenger/apache on centos 6. I installed all the ruby/rails/passenger stuff, so its possible something isn't setup properly.
Thanks
EDIT: Here is my routes.db file:
Rortest::Application.routes.draw do
get "inventing/index"
get "ideas/index"
root to: 'ideas#index'
end
tldr: Your problem is probably the route. Change get 'inventing/index' to get 'inventing/index'=> 'inventings#index' to correctly point the route to your controller action.
There are four things you need to do when adding a new page/route. I usually do them in this order for static pages:
1) Create a controller with the appropriate action for each page
You already did this with rails generate controller inventing index, but make sure that:
In your app/controllers folder, you do indeed have a file called inventings_controller.rb
In inventings_controller.rb, you have at least this:
class InventingsController < ApplicationController
def index
end
end
2) Create a view for each controller action
Make sure that:
In your app/views/inventings folder, you have a file called index.html.erb
Puts some simple HTML in that file, like <h1>Testing123</h1>, just so you can see if it's working.
3) Add a route for each controller action
It looks like this may be the problem. In your routes.rb file, you'll need this:
get 'inventing/index' => 'inventings#index'
Your root to works because it's actually pointing directly to your controller action, but rails isn't smart enough to guess that inventing/index should go to the index action in your inventing**s** controller.
4) As others have suggested, restart your rails app
You can do this with ctrl+c at the command line, then rails s again to start it back up.

Issue with controller generation in rails

New to Ruby on Rails so this may be a stupid question. I have an app and I can bundle my gems without issue. So now I want to add some mostly static pages. I try to generate a controller for them with rails generate controller MostlyStatic page1 page2. This should generate a controller named mostly_static and pages named page1 and page2. Instead, I throw an error. Apparently the generate command is trying to connect to the database, which I have not yet created. There is nothing in these pages that should be a database table, so I'm a bit confused as to why the database is being brought into the process at this juncture. I've looked through various tutorials and none say that a database is required to generate controllers for static pages. So... what am I missing? Do I need to create the database first just to generate static pages? And, if so, will subsequently dropping any tables created by that generation impair the function of my app? I really don't want a bunch of useless tables for static pages hanging around. Is there a way to generate these pages and controllers without the database?
You are not following the convention for generating controllers. Generating a controller will not create a database table. You have to do that by calling rails generate model, rails generate resource or rails generate scaffold.
So you want a controller for a few static pages. Try this
rails generate controller static_pages home help contact
Notice the generator is plural and snake case (static_pages). this will generate the static controller and the home.html.erb, help.html.erb, and contact.html.erb pages
Now you can access the pages with these actions in the controller
def home
end
def help
end
def contact
end
Also need to make sure the routes are set up
# routes.rb
match '/home', to: 'static_pages#home'
match '/help', to: 'static_pages#help'
match '/contact', to: 'static_pages#contact'
No database is set up and you can visit the pages. Thats all you need to do. just follow the conventions,like plural controllers and singular models and rails takes care of the details. Hope this gets you started
UPDATE
in response to the comments here is the standard output of generating a controller. Note my example used haml instead of erb, but there is nothing related to the database in the output.
rails g controller static_pages home help contact
create app/controllers/static_pages_controller.rb
route get "static_pages/contact"
route get "static_pages/help"
route get "static_pages/home"
invoke haml
create app/views/static_pages
create app/views/static_pages/home.html.haml
create app/views/static_pages/help.html.haml
create app/views/static_pages/contact.html.haml
invoke rspec
create spec/controllers/static_pages_controller_spec.rb
create spec/views/static_pages
create spec/views/static_pages/home.html.haml_spec.rb
create spec/views/static_pages/help.html.haml_spec.rb
create spec/views/static_pages/contact.html.haml_spec.rb
invoke helper
create app/helpers/static_pages_helper.rb
invoke rspec
create spec/helpers/static_pages_helper_spec.rb
invoke assets
invoke coffee
create app/assets/javascripts/static_pages.js.coffee
invoke scss
create app/assets/stylesheets/static_pages.css.scss
For anyone stumbling across this question, the correct answer is that the database need not exist, but it must be properly configured as if it did exist in the config file. Generating the controller does not actually create the database.

error on rails 3

Hi this is ROR beginner's question.
I have creat controller.rb and view hello.rhtml following the tutorial, but when I try to open localhost:3000/say/hello, it come up with with error: No route matches [GET] "/say/hello"
could any one advice please?
Well you need to setup a route for that in your config/routes.rb file.
For first try i would say use a script generator, enter on the command line as being in the project library > rails g controller helloworld index. This will create a route for itself, and a controller file.
After this script runs, there should be a line in your config/routes.rb
Cloud::Application.routes.draw do
get "helloworld/index"
end
Then you need to enter localhost:3000/helloworld/index in your browser url bar. Then ( as default ) rails will render the view located in app/views/helloworld/index.*. If you want to change this behaviour, go to the helloworld controller.
For more info there is a useful guide: ROUTING GUIDES
You need to define a route definition so that the URL you are requesting gets mapped to an action in the controller you have created which would render hello.rhtml. Say your controller name is says_controller.rb (thats how Rails gives the filename). In that if you define and action hello which would by default render hello.rhtml, then the fallback routes which are defined in the routes.rb file at the end would make a request to say/hello to look for the say_controller and hello action, thus rendering hello.rhtml.
For detailed help you can refer to the Rails Guides. There is a lot of helpful material and it is explained very well.
I started developing RoR recently and the best practise I got was the tutorial # rails for zombies and video's # railscasts. I suggest you watch some / make some and you get a general idea how to get started :)
-edit- on this issue: You're trying to render the hello view from the say controller.
since routing is handled by default on :controller/:action, do you have a action called hello in say? No action means no route means no view rendered.
class SayController < ApplicationController
def hello
#do nothing or add some code
logger.debug "I'm in the say controller, hello action!"
end
end
This should get it to render the hello file. You might want to take a look at restful actions / crud though, rails uses those by default.

The symbol "-" creates issue when used in page names in Ruby On Rails

I am new to Ruby on rails and working in a new test application. Consider my application url is : www.test.com
I would like to place a page by creating controller and respective views, the page should look like the below
www.test.com/articles/book-test-page
I created a controller "article" (defined the action as
def book_test_page
...
...
end
and folder under view as view/articles/book-test-page.
What routing information should i add in the routes.rb file to make this url bring up the specified url work? Any suggestion would be helpful..
Thanks,
Balan
get 'articles/book-test-page', :to => 'articles#book_test_page'

ruby on rails sub directory inside in the 'Views' main directory

am a newbie in ruby on rails and am stuck with a simple problem of routing.
I have my controller 'sub' and the 'Views' folder containing the add,edit,new erb files.
In my routes file, i have 'map.resources :subs'.
Until now, everything is fine.
Problem:
I moved the add,edit,new erb files into a subfolder called 'admin' inside the 'Views' main directory.
I have no idea how to call those erb files from that 'admin' subdir.
By default, it is looking for /app/views/subs/index.html.erb, and i want it to look in /app/views/subs/admin/index.html.erb
Please can anyone tell me how to do this.
Many many thanks
I suggest a different approach because it seems what you want to do is admin routing. In your routes.rb write
namespace :admin do
resources :subs
end
then put your views in the subdirectory views/admin/subs
also, put your controller in the subdirectory controllers/admin and namespace them with "Admin" too, e.g.
class Admin::StubsController < Admin::ApplicationController
your_code_goes_here
end
of course, then you need an application_controller.rb in the controllers/admin dir as well. But you cold also derive from ApplicationController then that is not necessary.
your controller can be called through the url /admin/subs
does that help?
You could explicitly render your templates within your controller actions, like this:
render :template => "subs/admin/index"
I'm a beginner in RoR.
What I wanted was to group all views (such as a mobile friendly version) in 1 folder but not end up with an extra namespace OR create new method in controllers. localhost:3000/posts calls:
class Post < ActiveRecord
and not
class Admin::Post < ActiveRecord
BUT load the views in views/android/posts/index.html.erb
Because this was my Google first hit, the link below is to an alternative answer that took sometime for me to find.
Rails: Elegant way to structure models into subfolders without creating submodules

Resources