Rails 4 Routing No route matches [GET] "/help" - ruby-on-rails

I have no idea what mistake I've made this is so basic:
Localhost:3000 works as my root, but if I put localhost:3000/help I get the following error:
Started GET "/help" for 127.0.0.1 at 2016-02-09 17:11:37 -0500
ActionController::RoutingError (No route matches [GET] "/help"):
web-console (2.0.0.beta3) lib/action_dispatch/debug_exceptions.rb:22:in `middleware_call'
web-console (2.0.0.beta3) lib/action_dispatch/debug_exceptions.rb:13:in `call'
actionpack (4.2.2) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
railties (4.2.2) lib/rails/rack/logger.rb:38:in `call_app'
railties (4.2.2) lib/rails/rack/logger.rb:20:in `block in call'
activesupport (4.2.2) lib/active_support/tagged_logging.rb:68:in `block in tagged'
activesupport (4.2.2) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (4.2.2) lib/active_support/tagged_logging.rb:68:in `tagged'
railties (4.2.2) lib/rails/rack/logger.rb:20:in `call'
actionpack (4.2.2) lib/action_dispatch/middleware/request_id.rb:21:in `call'
rack (1.6.4) lib/rack/methodoverride.rb:22:in `call'
rack (1.6.4) lib/rack/runtime.rb:18:in `call'
activesupport (4.2.2) lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'
rack (1.6.4) lib/rack/lock.rb:17:in `call'
actionpack (4.2.2) lib/action_dispatch/middleware/static.rb:113:in `call'
rack (1.6.4) lib/rack/sendfile.rb:113:in `call'
railties (4.2.2) lib/rails/engine.rb:518:in `call'
railties (4.2.2) lib/rails/application.rb:164:in `call'
rack (1.6.4) lib/rack/lock.rb:17:in `call'
rack (1.6.4) lib/rack/content_length.rb:15:in `call'
rack (1.6.4) lib/rack/handler/webrick.rb:88:in `service'
/home/reed/.rubies/ruby-2.1.5/lib/ruby/2.1.0/webrick/httpserver.rb:138:in `service'
/home/reed/.rubies/ruby-2.1.5/lib/ruby/2.1.0/webrick/httpserver.rb:94:in `run'
/home/reed/.rubies/ruby-2.1.5/lib/ruby/2.1.0/webrick/server.rb:295:in `block in start_thread'
Here is my routes.rb:
Rails.application.routes.draw do
root 'static_pages#home'
get 'static_pages/help'
get 'static_pages/about'
end
I have the file help.html.erb under views/static_pages, below are the file contents:
<% provide(:title, "Help") %>
<h1>StaticPages#help</h1>
<p>Find me in app/views/static_pages/help.html.erb</p
Here is my controller:
class StaticPagesController < ApplicationController
def home
end
def help
end
end
This is about as basic as a rails app gets. I don't know what I've done, but if you can see the typo or wherever I have made a mistake please let me know. Thanks

Your syntax is off a bit.
You have:
Rails.application.routes.draw do
root 'static_pages#home'
get 'static_pages/help'
get 'static_pages/about'
end
It should be:
Rails.application.routes.draw do
root 'static_pages#home'
get 'help' => 'static_pages#help'
get 'about' => 'static_pages#about'
end
You can see the relevant rails documentation on this at this link

Related

Rails 4 No route matches [GET] when clicking on a link to open a PDF

In my Rails 4 application, I'm showing a link to the PDF files. The user should be able to click the link and see the PDF.
What I got is the error No route matches [GET]: "/assets/documents/pdf/..."
I don't know what to do if I need to add in the routes and controller something?
My controller:
class SearchesController < ApplicationController
require 'net/http'
def index
if params[:search].present?
url = "http://localhost:3003/api/search?q=" + params[:search][:keyword]
uri = URI(url)
resp = Net::HTTP.get(uri)
#json_resp = JSON.parse(resp)
#result = #json_resp["docs"]
end
end
def search
end
end
The view:
<b>Pdf:</b> <%= link_to item["resource_name"], asset_path("/assets/documents/pdf/" + item["resource_name"]), :target => "_blank", :class => "links" %>
Log:
Started GET "/show_pdf/%2Fassets%2Fdocuments%2Fpdf%2F2018%2003%20Technical%20Conformance%20Guide%20v4.1.pdf" for 127.0.0.1 at 2018-08-07 14:34:11 +0200
ActionController::RoutingError (No route matches [GET] "/show_pdf/%2Fassets%2Fdocuments%2Fpdf%2F2018%2003%20Technical%20Conformance%20Guide%20v4.1.pdf"):
actionpack (4.2.6) lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'
web-console (2.3.0) lib/web_console/middleware.rb:28:in `block in call'
web-console (2.3.0) lib/web_console/middleware.rb:18:in `catch'
web-console (2.3.0) lib/web_console/middleware.rb:18:in `call'
actionpack (4.2.6) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
railties (4.2.6) lib/rails/rack/logger.rb:38:in `call_app'
railties (4.2.6) lib/rails/rack/logger.rb:20:in `block in call'
activesupport (4.2.6) lib/active_support/tagged_logging.rb:68:in `block in tagged'
activesupport (4.2.6) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (4.2.6) lib/active_support/tagged_logging.rb:68:in `tagged'
railties (4.2.6) lib/rails/rack/logger.rb:20:in `call'
quiet_assets (1.1.0) lib/quiet_assets.rb:27:in `call_with_quiet_assets'
actionpack (4.2.6) lib/action_dispatch/middleware/request_id.rb:21:in `call'
rack (1.6.10) lib/rack/methodoverride.rb:22:in `call'
rack (1.6.10) lib/rack/runtime.rb:18:in `call'
activesupport (4.2.6) lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'
rack (1.6.10) lib/rack/lock.rb:17:in `call'
actionpack (4.2.6) lib/action_dispatch/middleware/static.rb:120:in `call'
rack (1.6.10) lib/rack/sendfile.rb:113:in `call'
railties (4.2.6) lib/rails/engine.rb:518:in `call'
railties (4.2.6) lib/rails/application.rb:165:in `call'
rack (1.6.10) lib/rack/lock.rb:17:in `call'
rack (1.6.10) lib/rack/content_length.rb:15:in `call'
rack (1.6.10) lib/rack/handler/webrick.rb:88:in `service'
/Users/jakublemiszewski/.rvm/rubies/ruby-2.3.1/lib/ruby/2.3.0/webrick/httpserver.rb:140:in `service'
/Users/jakublemiszewski/.rvm/rubies/ruby-2.3.1/lib/ruby/2.3.0/webrick/httpserver.rb:96:in `run'
/Users/jakublemiszewski/.rvm/rubies/ruby-2.3.1/lib/ruby/2.3.0/webrick/server.rb:296:in `block in start_thread'
Controller Action
def inline_pdf
// read the params from the routes item["resource"]
and then read the full path of the file by
// File.expand_path(item["resource"])
send_data(File.expand_path(item["resource"], disposition: 'inline', type: 'application/pdf')
end
View
<%= link_to item["resource_name"], inline_pdf_path(item["resource_name"])%>
routes
get "/inline_pdf/:name" => "controller_name#inline_pdf", as: :inline_pdf

Rails 'button_to' is not working with 'send_file' method

In the view, suppose I have a button. If I click on it, it should trigger the file download.
In my view I have:
<%= link_to 'Download File', download_file_home_path %>
In routes.rb
resources :homes, only: [:update] do
member do
get 'download_file'
end
end
In homes_controller.rb
def download_file
send_file "path/to/file.pdf"
end
When I click on the link, everything works perfectly and it triggers download. But I need a button instead of link. When I use the button_to method, it fails and I get following error:
ActionController::RoutingError (No route matches [POST] "/source_machines/4/download_report"):
actionpack (4.2.5) lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'
newrelic_rpm (3.16.1.320) lib/new_relic/agent/instrumentation/middleware_tracing.rb:96:in `call'
actionpack (4.2.5) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
newrelic_rpm (3.16.1.320) lib/new_relic/agent/instrumentation/middleware_tracing.rb:96:in `call'
railties (4.2.5) lib/rails/rack/logger.rb:38:in `call_app'
railties (4.2.5) lib/rails/rack/logger.rb:20:in `block in call'
activesupport (4.2.5) lib/active_support/tagged_logging.rb:68:in `block in tagged'
activesupport (4.2.5) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (4.2.5) lib/active_support/tagged_logging.rb:68:in `tagged'
railties (4.2.5) lib/rails/rack/logger.rb:20:in `call'
newrelic_rpm (3.16.1.320) lib/new_relic/agent/instrumentation/middleware_tracing.rb:96:in `call'
actionpack (4.2.5) lib/action_dispatch/middleware/request_id.rb:21:in `call'
newrelic_rpm (3.16.1.320) lib/new_relic/agent/instrumentation/middleware_tracing.rb:96:in `call'
rack (1.6.4) lib/rack/methodoverride.rb:22:in `call'
newrelic_rpm (3.16.1.320) lib/new_relic/agent/instrumentation/middleware_tracing.rb:96:in `call'
rack (1.6.4) lib/rack/runtime.rb:18:in `call'
newrelic_rpm (3.16.1.320) lib/new_relic/agent/instrumentation/middleware_tracing.rb:96:in `call'
activesupport (4.2.5) lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'
newrelic_rpm (3.16.1.320) lib/new_relic/agent/instrumentation/middleware_tracing.rb:96:in `call'
rack-rewrite (1.5.1) lib/rack/rewrite.rb:24:in `call'
newrelic_rpm (3.16.1.320) lib/new_relic/agent/instrumentation/middleware_tracing.rb:96:in `call'
rack (1.6.4) lib/rack/lock.rb:17:in `call'
newrelic_rpm (3.16.1.320) lib/new_relic/agent/instrumentation/middleware_tracing.rb:96:in `call'
rack (1.6.4) lib/rack/sendfile.rb:113:in `call'
newrelic_rpm (3.16.1.320) lib/new_relic/agent/instrumentation/middleware_tracing.rb:96:in `call'
railties (4.2.5) lib/rails/engine.rb:518:in `call'
railties (4.2.5) lib/rails/application.rb:165:in `call'
railties (4.2.5) lib/rails/railtie.rb:194:in `public_send'
railties (4.2.5) lib/rails/railtie.rb:194:in `method_missing'
newrelic_rpm (3.16.1.320) lib/new_relic/agent/instrumentation/middleware_tracing.rb:96:in `call'
rack (1.6.4) lib/rack/deflater.rb:35:in `call'
newrelic_rpm (3.16.1.320) lib/new_relic/agent/instrumentation/middleware_tracing.rb:96:in `call'
rack (1.6.4) lib/rack/content_length.rb:15:in `call'
puma (3.6.0) lib/puma/configuration.rb:225:in `call'
puma (3.6.0) lib/puma/server.rb:578:in `handle_request'
puma (3.6.0) lib/puma/server.rb:415:in `process_client'
puma (3.6.0) lib/puma/server.rb:275:in `block in run'
puma (3.6.0) lib/puma/thread_pool.rb:116:in `call'
puma (3.6.0) lib/puma/thread_pool.rb:116:in `block in spawn_thread'
logging (2.1.0) lib/logging/diagnostic_context.rb:450:in `call'
logging (2.1.0) lib/logging/diagnostic_context.rb:450:in `block in create_with_logging_context'
My question is why does it works with link_to and does not work with button_to method, and what can I do to make it work with button_to
Ok, try it:
<%= button_to 'your_button_name', your_path, method: 'get' %>
Following #prashant 's comment,
This is because your route for the action is created for GET request and your link is sending GET request so it working.But button_to is sending request as POST request which is not matching to any route.
So adding method: 'get' fixed the problem.
The view should look like following:
<%= button_to 'Download Report', download_file_home_path, method: 'get' %>

Ruby On Rails throws error on home - railties

When I try my page i.e, localhost:3000/home - it shows this error.
This is the error thrown in the console:
ActionController::RoutingError (No route matches [GET] "/home"):
web-console (2.0.0.beta3) lib/action_dispatch/debug_exceptions.rb:22:in `middleware_call'
web-console (2.0.0.beta3) lib/action_dispatch/debug_exceptions.rb:13:in `call'
actionpack (4.2.2) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
railties (4.2.2) lib/rails/rack/logger.rb:38:in `call_app'
railties (4.2.2) lib/rails/rack/logger.rb:20:in `block in call'
activesupport (4.2.2) lib/active_support/tagged_logging.rb:68:in `block in tagged'
activesupport (4.2.2) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (4.2.2) lib/active_support/tagged_logging.rb:68:in `tagged'
railties (4.2.2) lib/rails/rack/logger.rb:20:in `call'
actionpack (4.2.2) lib/action_dispatch/middleware/request_id.rb:21:in `call'
rack (1.6.4) lib/rack/methodoverride.rb:22:in `call'
rack (1.6.4) lib/rack/runtime.rb:18:in `call'
activesupport (4.2.2) lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'
I think there is some problem with the railties. But I am not sure what it is?
content of my routes.rb :
Rails.application.routes.draw do
get 'static_pages/home'
get 'static_pages/help'
end
You should define your route like this:
get 'home' => 'static_pages#home'
or:
root 'static_pages#home'
If you want to make it root path for your application.

Getting routing error time to time:"No routes matches..."

I have created a simple application with CRUD operations and application is working fine. But time to time i get an error saying "ActionController::RoutingError (No route matches [GET]...)".
When i restart the server,application starts working again.
Full Trace:
actionpack (4.1.1) lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'
actionpack (4.1.1) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
railties (4.1.1) lib/rails/rack/logger.rb:38:in `call_app'
railties (4.1.1) lib/rails/rack/logger.rb:20:in `block in call'
activesupport (4.1.1) lib/active_support/tagged_logging.rb:68:in `block in tagged'
activesupport (4.1.1) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (4.1.1) lib/active_support/tagged_logging.rb:68:in `tagged'
railties (4.1.1) lib/rails/rack/logger.rb:20:in `call'
actionpack (4.1.1) lib/action_dispatch/middleware/request_id.rb:21:in `call'
rack (1.5.2) lib/rack/methodoverride.rb:21:in `call'
rack (1.5.2) lib/rack/runtime.rb:17:in `call'
activesupport (4.1.1) lib/active_support/cache/strategy/local_cache_middleware.rb:26:in `call'
rack (1.5.2) lib/rack/lock.rb:17:in `call'
actionpack (4.1.1) lib/action_dispatch/middleware/static.rb:64:in `call'
rack (1.5.2) lib/rack/sendfile.rb:112:in `call'
railties (4.1.1) lib/rails/engine.rb:514:in `call'
railties (4.1.1) lib/rails/application.rb:144:in `call'
rack (1.5.2) lib/rack/lock.rb:17:in `call'
rack (1.5.2) lib/rack/content_length.rb:14:in `call'
rack (1.5.2) lib/rack/handler/webrick.rb:60:in `service'
/Users/shobhitgarg/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/webrick/httpserver.rb:138:in `service'
/Users/shobhitgarg/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/webrick/httpserver.rb:94:in `run'
/Users/shobhitgarg/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/webrick/server.rb:295:in `block in start_thread'
Could anyone please help?
Adding Files:
routes.rb
require Rails.root.join("config/routes/testmin_routes.rb")
Rails.application.routes.draw do
root 'welcome#index'
end
testmin_routes.rb
Rails.application.routes.draw do
match 'admin/test_group/new' ,to:'admin/test_group#new',:via => :GET
match 'admin/test_group/create' ,to:'admin/test_group#create' , :via => :POST
match 'admin/test_group/edit' ,to:'admin/test_group#edit' , :via => :GET
match 'admin/test_group' ,to: 'admin/test_group#index', :via => :GET
match 'admin/test_group/update' ,to: 'admin/test_group#update', :via => :PUT
match 'admin/test_group/destroy' ,to: 'admin/test_group#destroy', :via =>:DELETE
end

Ruby on rails routing error No route matches [POST] "/index.html"

I have a rails app where all the feature specs pass for signing up however for some reason when I run rails server I keep getting this error
Started POST "/index.html" for 127.0.0.1 at 2013-10-03 22:17:29 +0300
ActionController::RoutingError (No route matches [POST] "/index.html"):
actionpack (4.0.0) lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'
actionpack (4.0.0) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
railties (4.0.0) lib/rails/rack/logger.rb:38:in `call_app'
railties (4.0.0) lib/rails/rack/logger.rb:21:in `block in call'
activesupport (4.0.0) lib/active_support/tagged_logging.rb:67:in `block in tagged'
activesupport (4.0.0) lib/active_support/tagged_logging.rb:25:in `tagged'
activesupport (4.0.0) lib/active_support/tagged_logging.rb:67:in `tagged'
railties (4.0.0) lib/rails/rack/logger.rb:21:in `call'
actionpack (4.0.0) lib/action_dispatch/middleware/request_id.rb:21:in `call'
rack (1.5.2) lib/rack/methodoverride.rb:21:in `call'
rack (1.5.2) lib/rack/runtime.rb:17:in `call'
activesupport (4.0.0) lib/active_support/cache/strategy/local_cache.rb:83:in `call'
rack (1.5.2) lib/rack/lock.rb:17:in `call'
actionpack (4.0.0) lib/action_dispatch/middleware/static.rb:64:in `call'
railties (4.0.0) lib/rails/engine.rb:511:in `call'
railties (4.0.0) lib/rails/application.rb:97:in `call'
rack (1.5.2) lib/rack/lock.rb:17:in `call'
rack (1.5.2) lib/rack/content_length.rb:14:in `call'
rack (1.5.2) lib/rack/handler/webrick.rb:60:in `service'
/Users/brozturk/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/webrick/httpserver.rb:138:in `service'
/Users/brozturk/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/webrick/httpserver.rb:94:in `run'
/Users/brozturk/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/webrick/server.rb:295:in `block in start_thread'
Here are my routes
resources :users
root 'users#new'
Why would the app try to post index.html?And why would my tests pass but when I run the server it would not work?I had devise installed but then decided not to use it so I did a git reset --hard and continued that way.could that be the issue?How can I fix this?

Resources