I'm new in Ruby On Rails. I installed in 2 days ago on windows and now I want to create a simple "Hello Rails" with it. I use This Tutorial. I did all the steps described in this it does'nt work. the steps are:
write this command in PowerShell: rails generate controller home index
Open app/views/home/index.html.erb in text editor and edit it to contain a single line
of code: Hello, Rails!
delete the default page from application with this command
rm public/index.html
Open the file config/routes.rb in editor, and edit root
:to => "welcome#index"
to
:to => "home#index"
navigate to
"http://localhost:3000"
in my browser but I see this error instead Hello Rails:
(I don't have enough reputation to post image of error)
Routing Error
No route matches [GET] "/"
Can anybody tell me whats the problem?
Thanks
Make sure the only content in your config/routes.rb file is the following:
Blog::Application.routes.draw do
root :to => "home#index"
end
(just delete everything else for now to make sure it's clean)
Also, try killing the server and making sure that you don't still see that error message (which could indicate that another server is still running somewhere). Along these lines, make sure you see the request in your console output when you fetch the page.
If you're still getting the error, look for any clues in the console error messages.
Related
When I run the rails server it returns with an error in index_html.erb. the code ran great for a few minutes then I came across this error.
routes.rb file
[
[
Rails uses the routes.rb file to locate the various resources of your application, so any resource you expect your users to access will have to be defined in routes.rb. You don't have a route defined for home_about_path.
From your code, I take it you have an about method in your home_controller.rb controller file. You have to have the following line in your routes.rb (add it under 'get home#index'):
get 'home#about' => 'home/about'
Adding this line should create a home_about_path and your link_to method should work.
Understanding routing is incredibly important to Rails development, so you should do some reading and make sure you have a good grasp of the fundamentals before pushing too far ahead.
I am new in RoR. I just added a new page hello.html in public folder. When I try to load the page on browser I get following error
No route matches [GET] "/hello.html"
Try running rake routes for more information on available routes.
I found some solution so far but they don't really working for me. I tried following lines in routes.rb file
match '/hello', to: '#hello'
match 'hello', to: 'hello'
match '/hello' => '#hello', :as => 'hello'
But Im getting same error. I know that for files in public folder I don't need to add routes but Still I get the same error.
For any files added in the public folder, you don't need to add routes. If the file is available as
public/hello.html
Its accessible via localhost:3000/hello.html
Simple! No routes.
I am creating a simple suggestion box app (to learn Rails) and am getting the following Rails routing error when I go to "/suggestion-boxes" running on my local machine (localhost:3000)
"Routing Error
No route matches [GET] "/suggestion-boxes"
In my routes.rb file I have:
SuggestionBoxApp::Application.routes.draw do
resources :suggestion_boxes
end
This is what I get when I run rake routes:
suggestion-box-app$ rake routes
suggestion_boxes GET /suggestion_boxes(.:format) suggestion_boxes#index
POST /suggestion_boxes(.:format) suggestion_boxes#create
new_suggestion_box GET /suggestion_boxes/new(.:format) suggestion_boxes#new
edit_suggestion_box GET /suggestion_boxes/:id/edit(.:format) suggestion_boxes#edit
suggestion_box GET /suggestion_boxes/:id(.:format) suggestion_boxes#show
PUT /suggestion_boxes/:id(.:format) suggestion_boxes#update
DELETE /suggestion_boxes/:id(.:format) suggestion_boxes#destroy
However, if I modify my routes file to
SuggestionBoxApp::Application.routes.draw do
get "suggestion-boxes" => "suggestion_boxes#index"
end
Then the page "/suggestion-boxes" displays as per the index action in my SuggestionBoxesController.
I tried restarting my server but this had no impact. While I of course can go with using GET, this error makes no sense, and I would like understand what is causing it.
Any insights would be very much appreciated.
The error is that you are not renaming the REST route, instead the controller one.
Try declaring
resources :suggestion_boxes, :as => "suggestion-boxes"
in your config/routes.rb file.
Somewhere in your code you are calling to suggestion-boxes controller which does not exist. Your controller is suggestion_boxes, spelling. So where every you have "suggestion-boxes" you should replace with "suggestion_boxes". The code that you added create an alias that matches 'suggestion-boxes' to the index action of the suggestion_boxes controller so this resolves it if it is your desired affect. But simply fixing your spelling would resolve your problem. I usually use the second approach if I want the change the URL that user see. Have a look at the routing doc for a better understanding
On visiting localhost:3000 I'm getting the typical:
No route matches [GET] "/"
I don't want to see that error screen. I already trying with localhost:3000/passbook/v1/passes/ but still nothing so I typed:
rake routes
and got this output:
passbook GET /passbook/v1/passes/:pass_type_identifier/:serial_number(.:format) passbook/passes#show {:pass_type_identifier=>/([\w\d]\.?)+/}
GET /passbook/v1/devices/:device_library_identifier/registrations/:pass_type_identifier(.:format) passbook/registrations#index {:pass_type_identifier=>/([\w\d]\.?)+/}
POST /passbook/v1/devices/:device_library_identifier/registrations/:pass_type_identifier/:serial_number(.:format) passbook/registrations#create {:pass_type_identifier=>/([\w\d]\.?)+/}
DELETE /passbook/v1/devices/:device_library_identifier/registrations/:pass_type_identifier/:serial_number(.:format) passbook/registrations#destroy {:pass_type_identifier=>/([\w\d]\.?)+/}
passbook_log POST /passbook/v1/log(.:format) passbook/logs#create
How can I resolve this?
You haven't told Rails yet what your root path is. You can do this by saying
root to: "passes#show"
in config/routes.rb. Afterwards, you should see this in the rake routes output:
root / passes#show
Then it should work!
Additional tip
This may be over the top for your question, but following the test driven approach you should write a spec first:
describe "custom routing" do
it "shows the root page" do
get("/").should route_to("passes#show")
end
end
Use RSpec for this. Run the test in your console before having written the route itself by doing $ rspec and see the test fail at the right place.
Then, implement the routes above, run the test again—it should work. This way you ensure that you have written just enough code to meet your requirements, and that it is really the code you have written that lets you access the root path.
Is this a rails app you inherited from someone else?
Given the rake routes output you have provided, a url like localhost:3000/passbook/v1/passes/<EXAMPLE_PASS_TYPE_IDENTIFIER>/<EXAMPLE_SERIAL_NUMBER> should work based on the rule in the first line of the output, provided you replace EXAMPLE_PASS_TYPE_IDENTIFIER and EXAMPLE_SERIAL_NUMBER with valid values from the database.
The error message No route matches [GET] "/ for localhost:3000 is because there is no mapping for root in rake routes output.
You could modify the config/routes.rb file and add the following line at the bottom of the file:
root :to => "<controller_name>#<action_name>"
Eg:
root :to => "passbook/index"
if there is a passbooks_contoller.rb with a index method.
Best to learn more about rails routing from the documentation. Even better go through the Rails Tutorial to get a good understanding of the rails framework.
I'm using RailsSpace to learn Ruby on Rails and am coming across an error after performing what seems like a simple command.
I used the Terminal to generate a new User controller with the views Index and Register:
$ rails generate controller User index register
And it had no problem with that, creating the files index.html.erb and register.html.erb as well as all the other expected files.
But when I visit http://localhost:3000/user/register, it comes back with the error message:
ROUTING ERROR: No route matches {:controller=>"user",
:action=>"about"}
My routes.rb doesn't indicate any abnormalities:
RailsSpace::Application.routes.draw do
get "user/index"
get "user/register"
get "site/index"
get "site/about"
get "site/help"
root :to => "site#index"
end
Why does it try to route to the "About" action, and what other file can I edit to change this routing?
I'm using Rails 3 in case that matters.
I would try hand coding in the route. In your case it would look like this:
match '/user/register' => 'users#register', :as => :register
This will definitely work and prevent the page /user/register from going to the about page. Let me know how things go and Ill try to continue to steer you in the right direction.