Rails does not route to root 'welcome.index' - ruby-on-rails

ruby -v = 2.1.6p336
rails -v = 4.2.1
MySQL --version = 14.14
New to programming and Rails. Going through tutorials.
did rails generate controller welcome index.
Rails Server - localhost:3000 shows Welcome aboard page.
Change config/routes.rb file... un-comment out root 'welcome#index' (got rid of the #. (line 8)
Rails Server - localhost:3000 shows error.
----------copied from localhost:3000------------------------------------------
ExecJS::ProgramError in Welcome#index
Showing c:/row/dev/readit/app/views/layouts/application.html.erb where line #6 raised:
TypeError: Object doesn't support this property or method
Rails.root: c:/row/dev/readit
Application Trace | Framework Trace | Full Trace
app/views/layouts/application.html.erb:6:in `_app_views_layouts_application_html_erb___173287605_49138164'
commented the root 'welcome#index' out again, and I have the Welcome Aboard page back.
If I execute localhost:3000/index
I get a routing error. No route matches [GET] "/index"
I have:
controllers/welcome_controller.rb
views/welcome/index.html.erb
this is the start of the config/routes.rb file -----------------
Rails.application.routes.draw do
get 'welcome/index'
# The priority is based upon order of creation: first created -> highest priority.
# See how all your routes lay out with "rake routes".
# You can have the root of your site routed with "root"
root 'welcome#index'
How do I get the config/routes.rb file to display the views/welcome/index.html.erb file?
Does this have anything to do with the MySQL socket I'm suppose to put in the config/database.yml ? Because I don't what to put there.

It looks like there is an error at line 6 on "application.html.erb". If you want to create a hyperlink from that page to the "root" page, you should use the following view helper:
<%= link_to "Home", root_path %>
When the page is rendered and you check the HTML code using the Chrome's inspector, you will see that above code is converted to:
Home

In your app/views/layouts/application.html.erb, try changing the line:
<%= javascript_include_tag 'application', data-turbolinks-track => true %>
To this (removing the data-turbolinks-track => true):
<%= javascript_include_tag 'application' %>
That might fix it. You shouldn't remove the entire line as it's likely you'll need JS available in your layout at some point in the project :)
Hope it helps!

I fixed it by renaming my [It may not sound correct but it worked for me , give it a try]
app/views/layouts/application.html.erb
TO
app/views/layouts/default.html.erb
I hope it works for you as well

Related

how to add the home page as a root in rails version 2.3.14?

I am new to the rails programming.
I had a one problem to building a first rails application in rails version 2.3.14.
The problem is , I am not able to add the root in the config/routes.rb file.
I was created the home and about pages. I am trying to add the home page as a root. For that I was added the below line in the routes.rb file.
root 'pages#home'
But it gives error. The error is,
undefined method `root' for main:Object (NoMethodError)
It says the root is a undefined method. So, What method is used to add the home page as a root in rails version 2.3.14.
Use map.root and start upgrading your app to newer version of Rails :)
map.root :controller => "pages", :action => "home"

Inherit controller class from gem

I am using the ShopifyApp gem, which has an action called receive in its WebhooksController. As seen here: Webhooks controller
In my WebhooksController controller I am trying to override that receive action by doing the following:
class WebhooksController < ShopifyApp::WebhooksController
def receive
binding.pry
end
end
My route to my WebhooksController is this:
webhooks_receive POST /webhooks/receive(.:format) webhooks#receive
And the route put in by the Gem engine is:
webhooks POST /webhooks/:type(.:format) shopify_app/webhooks#receive
I see the data come in, but for some reason its not hitting my receive action and stopping at my pry and I'm not sure why.
Found this and tried it, but no luck.
I tried this solution too and it didn't work..
Any thoughts?
Here are the top of my logs showing what's happening:
Started POST "/webhooks/receive" for XX.XXX.37.116 at 2016-04-21 14:57:02
+0000
Cannot render console from XX.XXX.37.116! Allowed networks: XXX.0.0.1, ::1,
127.0.0.0/127.XXX.XXX.255
ActiveRecord::SchemaMigration Load (0.2ms) SELECT "schema_migrations".*
FROM "schema_migrations"
Processing by ShopifyApp::WebhooksController#receive as */*
Parameters: {"rate"=>{"origin"=>{"country"=>"US", "postal_code"=>"48615",
"province"=>"MI", "city"=>"Breckenridge", "name"=>nil, "address1"=>"6760..
bunch of data removed for brevity}}}
Completed 500 Internal Server Error in 6ms (ActiveRecord: 0.0ms)
NoMethodError (undefined method `variable_size_secure_compare' for
ActiveSupport::SecurityUtils:Module):
shopify_app (7.0.2) lib/shopify_app/webhook_verification.rb:20:in
`hmac_valid?'
And my routes file
Rails.application.routes.draw do
root :to => 'home#index'
mount ShopifyApp::Engine, at: '/'
resources :store
resources :shipping_methods
post '/webhooks/receive', to: 'webhooks#receive'
post '/billing_plans', to: 'billing_plans#save_plan', as: 'save_plan'
get '/activate_charge', to: 'billing_plans#activate_charge', as: 'activate'
post '/create_charge', to: 'billing_plans#create_charge', as:
'create_billing_plan'
After two days of troubleshooting I realized their Gemspec was including an old version of rails that did not contain the method they were calling..
I let them know and bumped my version from 4.2.5 to 4.2.6 and now it works.. frustrating but solved.. thanks everyone for your support!
If anyone else comes across this, in order to use the Shopify App gem Webhooks controller you will need to be running Rails 4.2.6.
Try putting routes in this order:
post '/webhooks/receive', to: 'webhooks#receive'
mount ShopifyApp::Engine, at: '/'
Rails routes are matched in the order they are specified, so if you
have a resources :photos above a get 'photos/poll' the show action's
route for the resources line will be matched before the get line. To
fix this, move the get line above the resources line so that it is
matched first.
So in order for your own route to take precedence over the one that is defined in the ShopifyApp gem, you need to define it before mounting the ShopifyApp engine.
Moving your own route post '/webhooks/receive', to: 'webhooks#receive' before mount ShopifyApp::Engine, at: '/' should fix the issue.
Refer to Rails Routing from the Outside In for more info.

Additional html.erb file in View not Working in ruby on rails

I have a database I created in ruby on rails which automatically created some of the needed files but when I manually added an additional file in the views folder and try to access it in my browser .
This error appears
Routing Error
No route matches [GET] "/second/count"
Try running rake routes for more information on available routes.
I tried to correct this by putting a number sign in the routes.rb
Community::Application.routes.draw do
#resources :seconds
match ':controller(/:action(/:id))', :via => :get
The file I just created worked but then the files automatically created won't.
This error appears
NameError in Seconds#index
Showing c:/Sites/Community/app/views/seconds/index.html.erb where line #25 raised:
undefined local variable or method `new_second_path' for #<#<Class:0x456ee80>:0x37b9a98>
Extracted source (around line #25):
22:
23: <br />
24:
25: <%= link_to 'New Second', new_second_path %>
Rails.root: c:/Sites/Community
Application Trace | Framework Trace | Full Trace
app/views/seconds/index.html.erb:25:in `_app_views_seconds_index_html_erb__742794588_29296056'
app/controllers/seconds_controller.rb:7:in `index'
Request
Parameters:
None
Show session dump
Show env dump
Response
Headers:
None
For the helper new_second_path to work, you have to uncomment the line resources :seconds in the routes.
Unless you know what you are doing, I would comment the line match ... which is a catch-all.
Also: a path /second/count will not work, it should be /seconds/count. To explain this:
/second/count: with your match it will look for SecondController (singular!!) and action count
But the clean way to handle is to write:
resources :seconds do
collection do
get :count
end
end
This will allow you to write count_seconds_path in your view, which will render the correct link (/seconds/count).

HttpHelpers routing methods in mounted rails engine results in uninitialized constant "controller name"

My environment:
Rails 3.2.8
Ruby 1.9.3p194
Fedora 16 x86_64
This problem seems specific to Rails Engines.
It seems that when using the HttpHelpers in a Rails Engine's routes file, I get "uninitialized constant Controller" when accessing a route via a browser. But, if I use a URL matcher in the Engine's routes file, it routes correctly.
Here's how I created a failing example:
$ rails plugin new my_engine --mountable
$ cd my_engine
$ rails g controller things index
$ rails s -p 3005
The controller generator uses the HttpHelpers#get method by default, so at this point the Rails Engine's config/routes.rb file looks like:
MyEngine::Engine.routes.draw do
get "things/index"
end
And, the test/dummy application's config/routes.rb file looks like:
Rails.application.routes.draw do
mount MyEngine::Engine => "/my_engine"
end
So, I should be able to hit http://locahost:3005/my_engine/things/index and see the Things#index view from the Engine. But, instead in the browser I see:
Routing Error
uninitialized constant ThingsController
If I manually change the Engine's config/routes.rb file to:
MyEngine::Engine.routes.draw do
#get "things/index"
match "things/index" => "things#index"
end
... and hit http://locahost:3005/my_engine/things/index, I see the correct Things#index view.
I noticed that when I use the HttpHelpers#get method in the Engine's config/routes.rb file, and run rake routes from the test/dummy directory, I see:
$ rake routes
my_engine /my_engine MyEngine::Engine
Routes for MyEngine::Engine:
things_index GET /things/index(.:format) things#index
But, if I change the Engine's config/routes.rb file to use the URL matcher method, I see:
$ rake routes
my_engine /my_engine MyEngine::Engine
Routes for MyEngine::Engine:
things_index /things/index(.:format) my_engine/things#index
Notice that when using the URL matcher, the controller and action are correctly namespaced under the engine. While, when using the HttpHelpers#get, the controller and action seem to be non-namespaced.
So, my question: Am I doing something wrong here? Or, is this a bug?
Note: I searched the rails issues and didn't see anything directly related to this. Though I did see several other engine and routing issues.

getting a 500 server error when bring up a page in rails

So I am getting a 500 server error when attempting to bring up a rhtml page in rails. When I start the WEBrick server, I get the welcome to rails homepage. The name of the app is hello. I generated the controller from the command line and it looks like
class HelloController < ApplicationController
def there
end
end
I have my view (there.rhtml) in views/hello/there.rhtml. However the http://localhost:3000/hello/there gets a 500 sever error. I am currently running this on a vista box. Any ideas?
Have you defined the route ? It is defined in routes.rb . Also you can try checking the development logs to see what exactly is the issue .
could post the error log message? as Nm suggested it can be a route problem and if it is try to add this route in routes.rb file
map.connect '/hello/there', :controller => 'hello', :action => 'there'

Resources