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.
Related
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.
Just wanted to know, what does this line mean in the routes.rb file:
AppName::Application.routes.draw do
Please explain. I am new to Rails.
Have a read through this page.
Basically, within the block passed to Application.routes.draw (which is just a call to a method defined in ActionDispatch::Routing module within the Rails core framework), you define all the URLs/Paths that you want your Rails application to respond to.
You can see all these route definitions, by running:
rake routes
in your terminal.
It is the main routes file which defines the root and other paths for the link.
It is used as suppose you want to change your index page from default ruby on rails to your index page you make changes to file and add
root to: "controllername#index"
This file is also used to add the model to the application
resources: "model_name"
Apart from this you can also define links in your rails application
get 'courses/index'
So going from courses controller to view of the index.
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.
I just installed Ruby on Rails and created a scaffold called posts. RoR generated controllers and other required files for me.
I created a new method in posts_controller, but I can't access it. I looked at other methods that are in the controller and looks like I need to access them by /posts/[MY POST ID]/[MY METHOD NAME].
Assuming I created my custom method hello in the controller, how do i access it?
I looked at routes.rb, but there's no configuration for it.
Updated:
I understand that I can manually configure it in routes.rb, but how do all the other methods work? For example, I have "edit", and "update" methods in the "posts_controller.rb" controller. How do those two methods work without configuring routes?
# GET /posts/1/edit
def edit
#post = Post.find(params[:id])
end
I can't find a configuration that matches /posts/[0-9]/edit pattern.
The documentation you're looking for is Rails Routing From the Outside In. Once you've read this you'll understand everything Rails does to take your request and point it at method in your controller.
You need to add a route for it to routes.rb. For example:
# Previous routes
# resources :posts
# Updated routes
resources :posts do
get "hello", :on => :member
end
Have a look at this Rails guide about routing, it should help you understand Rails routing.
This will give you a good head start on the routes: http://guides.rubyonrails.org/routing.html
Not every method you make will have its own path, rails is built on the rest principle, and your scaffold created methods in the post controller that follow those paths, like index, show etc....
You can force your method to have a route added to it, but in reality you rarely actually need to do so as following the convention is far easier.
In Rails 3.x
match 'posts/hello' => 'posts#hello'
Available at example.com/posts/hello
When you used scaffold to generate post, it added a line resources :posts in your routes.rb file. That line configures routes for all the controller actions that were generated. As Caleb mentions above, not every action has a dedicated path. A single path can correspond to multiple actions because rails also takes into account the HTTP method. So, for instance, the path /posts with the HTTP method GET corresponds to the controller action index, while the same path with the HTTP method PUT corresponds to the controller action update. You can see these associations when you run rake routes from the console. I agree with Jordan and Caleb that the Rails Guides is a good read and will help you understand routes.
I created a controller and a model. The controller is called "Admin" and the model is called "Album". I edited database.yml with proper info and did the rake db:migrate command which didn't return any errors and did migrate the db inside schema.rb. Inside the controller I wrote:
class AdminController < ApplicationController
scaffold :album
end
Next I started my server and went to http://localhost:3000/admin but instead of seeing the typical CRUD page I get the following error:
app/controllers/admin_controller.rb:3
Request
Parameters:
None
Show session dump
---
flash: !map:ActionController::Flash::FlashHash
{}
Response
Headers:
{"cookie"=>[],
"Cache-Control"=>"no-cache"}
Any idea why?
That syntax for scaffolding has been deprecated for quite some time. Nowadays, rails (versions 2.x) use the following method to scaffold a resource:
script/generate scaffold Album title:string date:date ...
That generates the scaffolding views (in app/views), the controller (app/controllers), standard tests (in test/) and, crucially, the required routes to make scaffolding work.
I believe the rails dev team took away the old syntax ("scaffold :resource") because no real application would ever leave a scaffold untouched, ie. you will always need some kind of customization. With the new syntax you can leave it untouched, but it is also much easier to customize.
If you really need your controller to be named admins, you can change the file config/routes.rb after generating the scaffolding. It makes no sense, though: Why should the URI to create a new album be called "/admins/new"?
If you are trying to create an admin area for an image album app, you are probably looking for namespaces (so you can have multiple different resources, controllers and views inside the "admin" namespace). To create an album resource within the admin namespace, write:
script/generate scaffold Admin/Album title:string date:date
In that case, your controller will be accessible as http://host/admin/albums.
Hm,
Normally you would have a controller and a model called Admin and the same thing would be about Album,
Take a look at this quick screen cast how a blog is done using scaffolding;
Creating a web-blog
the script/generate command seems not to work, someone has to provide ./script/generate , I think its a linux directory issue, you have to explicitly say you are starting from the current directory (./). hope this helps someone avoid scratching his head