Ruby on rails first project Routing Error - ruby-on-rails

I installed Instant Rails on windows and tested using http://127.0.0.0:3000 on browser it showing WELCOME page.
Next : i started a sample application using commands
> rails hello
> ruby script/generate controller Hello index
with this it created a hello app
but i am when accessing http://127.0.0.0:3000/hello
it is giving me
Routing Error
No route matches "/hello" with {:method=>:get}

You should have a view called hello.html.erb in the views/Hello folder

You have to set config/routes.rb up:
map.resources :hello
http://guides.rubyonrails.org/routing.html

Related

Moving route from Rails Application to Rails Engine gem

I'm working on a project where I have a Rails application (rails 4.2.5) that incorporates a gem, which is a Rails Engine. I'm currently working on moving the route definitions from the main application to the gem. I have a controller test that exercises one of the routes. The test fails when I move the route definition over to the gem, but the route looks the same.
My gem is called CallCenter, and includes a controller called call_center_controller (with an action called reports_tab). This is what routes.rb looks like in the Rails app:
...
mount CallCenter::Engine, at: "/call_center", as: 'call_center_urls'
get '/call_center/reports_tab' => 'call_center/call_center#reports_tab'
...
This is what I get running rake routes:
❯❯❯ rake routes | grep reports_tab
call_center_reports_tab GET /call_center/reports_tab(.:format) call_center/call_center#reports_tab
At this point controller test runs without errors. But when I move the route definition to the routes.rb file in the gem:
get 'call_center/reports_tab' => 'call_center#reports_tab'
(note that the route now points to 'call_center#reports_tab' instead of 'call_center/call_center#reports_tab', because the main application mounts the engine at /call_center)
Here is my output from rake routes, (still running from the main app's directory)
❯❯❯ rake routes | grep reports_tab
call_center_reports_tab GET /call_center/reports_tab(.:format) call_center/call_center#reports_tab
This is identical to the route reported by rake routes when it was defined in the main app, but now my test fails with the following message:
Failure/Error: get :reports_tab
ActionController::UrlGenerationError:
No route matches {:action=>"reports_tab", :controller=>"call_center/call_center"}
As far as I can tell the route is defined, and looks exactly the same as it did before when it was in the routes file for the main app. What am I missing?
I found the answer - I needed to add this to my controller spec to tell it to look at the routes provided by the Engine:
routes { CallCenter::Engine.routes }

Ruby On Rails - Controller and view blank page

I am stuck and unable to proceed on my Ruby on Rails tutorial.
I am doing a Ruby on Rails tutorial using a mac.
I have created a new Ruby on Rails project in Users/username/sites/simple_cms
I am using Webrick server
I started the server using rails s command and as expected, when navigating to the localhost:3000 the public index page was displayed.
I then added a controller and view using the following command in the route directory:
rails generate controller demo index
The controller and view were successfully created:
My routes file:
SimpleCms::Application.routes.draw do
get "demo/index"
My demo_controller File:
class DemoController < ApplicationController
def index
end
end
My View file is now located in views/demo/index.html.erb:
<h1>Demo#index</h1>
<p>hello world</p>
I then started Webrick again and entered:
http://localhost:3000/demo/index
but a blank page is displayed.
add root to 'demo#index' to routes file http://guides.rubyonrails.org/routing.html#using-root
Try to change your route as follows
get "index" => "demo#index"
then you can access the site with localhost:3000/index
But if this is your root path you can also do
root "demo#index"
to access the site with localhost:3000
In this case demo refers to your controller and index to your action in the controller.

RoR routing error in "hello world"

I'm trying to learn web-dev and decided to learn ruby on rails. I'm doing the first tutorial from "Agile Web Development with Rails". I've seen there are quite a few search results on google, but all the recommendation is "to restart the server", which does not help my case.
So, I started webrick and generate controller Say with these code:
def hello
end
and then I added hello.rhtml, which has some basic html with message "Hello from rails".
When i type in browser: http://localhost:3000/say/hello
I get : No route matches [GET] "/say/hello"
I tried to restart webrick, doesn't work. The bug-name suggest it has to do with routes, i've got two versions of the book, none of them have the steps to alter the routes.rb and within routes.rb i have commented-out stuff and class definition.
Does anyone know what should i do?
I'm guessing is a problem in your routes file. You need to define what you want rails to do (which controller+action you want to be perform) when that URL is received. So, do the following:
match 'say/hello', :to => "say#hello"
This will tell rails that when a URL with say/hello is received, the hello action of the controller say has to be performed.

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.

Rails routing error after fresh install

I just installed Rails, etc. on a Windows 7 machine.
Created my first app, and am trying to go to the first generated and this is what I get:
Routing Error
No route matches "/say/hello"
Where say is the app name, and hello is the name of the first view.
Thoughts?
If you're implementing very simple routes (and since this is your first app, I'm assuming that's what you want!), make sure you've un-commented the last route in routes.rb:
match ':controller(/:action(/:id(.:format)))'
This will send /say/hello to the hello action of the say controller.
Have you added route in config/routes.rb ?
get 'say#hello'

Resources