Routing Error during "Ruby on Rails-Tutorial" - ruby-on-rails

It seems like some people here had this problem but I couldn't find any solution in another topic.
I am doing Chapter 3 of the Ruby on Rails-Tutorial, working on the static pages. When I want to open them on the localhost it gives me a "Routing Error" in the Browser.
My Ruby is currently on version 1.9.3.
My Rails is currently on version 3.2.
I have tried:
restarting the server
saving all the files again
checking any issues in the static_pages_controller.rb
checking any issues in the routes.rb
checking any issues in the static_oages_spec.rb
Also there are no bugs in the HTML code of the single static page. And I can't find any more help in the tutorial, neither in other questions here on StackOverflow.
Edit:
This is the actual error message from the browser:
Routing Error
No route matches [GET] "/static_pages/home" Try running
rake routes for more information on available routes.
if I go to http://localhost:3000/static_pages/home, to one of three static pages I have.
This is routes.rb:
SampleApp::Application.routes.draw do
get "static_pages/home"
get "static_pages/help"
get "static_pages/about"
end
Also, I tried "rake routes" in the terminal, too. This is the result:
home_static_pages GET /static_pages/home(.:format) static_pages#home
help_static_pages GET /static_pages/help(.:format) static_pages#help
about_static_pages GET /static_pages/about(.:format) static_pages#about
static_pages POST /static_pages(.:format) static_pages#create
new_static_pages GET /static_pages/new(.:format) static_pages#new
edit_static_pages GET /static_pages/edit(.:format) static_pages#edit
GET /static_pages(.:format) static_pages#show
PUT /static_pages(.:format) static_pages#update
DELETE /static_pages(.:format) static_pages#destroy
And this is the error message the server is giving me:
Started GET "/static_pages/home.html" for 127.0.0.1 at 2012-04-03 13:23:54 +0200
ActionController::RoutingError (No route matches [GET] "/static_pages/home.html"):
actionpack (3.2.3) lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'
actionpack (3.2.3) lib/action_dispatch/middleware/show_exceptions.rb:56:in `call'
railties (3.2.3) lib/rails/rack/logger.rb:26:in `call_app'
railties (3.2.3) lib/rails/rack/logger.rb:16:in `call'
actionpack (3.2.3) lib/action_dispatch/middleware/request_id.rb:22:in `call'
rack (1.4.1) lib/rack/methodoverride.rb:21:in `call'
rack (1.4.1) lib/rack/runtime.rb:17:in `call'
activesupport (3.2.3) lib/active_support/cache/strategy/local_cache.rb:72:in `call'
rack (1.4.1) lib/rack/lock.rb:15:in `call'
actionpack (3.2.3) lib/action_dispatch/middleware/static.rb:62:in `call'
railties (3.2.3) lib/rails/engine.rb:479:in `call'
railties (3.2.3) lib/rails/application.rb:220:in `call'
rack (1.4.1) lib/rack/content_length.rb:14:in `call'
railties (3.2.3) lib/rails/rack/log_tailer.rb:14:in `call'
rack (1.4.1) lib/rack/handler/webrick.rb:59:in `service'
/Network/Servers/pluto.kayoom.lan/Users/benediktkrebs/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/1.9.1/webrick/httpserver.rb:138:in `service'
/Network/Servers/pluto.kayoom.lan/Users/benediktkrebs/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/1.9.1/webrick/httpserver.rb:94:in `run'
/Network/Servers/pluto.kayoom.lan/Users/benediktkrebs/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/1.9.1/webrick/server.rb:191:in `block in start_thread'

Try this :
match "/static_pages/home" => "static_pages#home", :via => :get
match "/static_pages/help" => "static_pages#help", :via => :get
match "/static_pages/about" => "static_pages#about", :via => :get
Add into routes , restart the server and refresh browser .

If you are using Spork you must re-run Spork so your tests will consider the changes in config files like routes.rb, that are pre-loaded with Spork, so are not automatically updated.
Stop Spork (Ctrl + C)
Run Spork again (bundle exec spork)
Source: this is the source of this information
"after changing a file included in the prefork loading (such as routes.rb), you will have to restart the Spork server to load the new Rails environment.
If your tests are failing when you think they should be passing, quit the Spork server with Control-C and restart it

your routes.rb file has problems, use either RESTful style:
resource :static_pages do
collection do
get :home
get :help
get :about
end
end
or the non RESTful style:
match "static_pages/home", :controller => "static_pages", :action => "home"
match "static_pages/help", :controller => "static_pages", :action => "help"
match "static_pages/about", :controller => "static_pages", :action => "about"
for more details please refer to official guide: http://guides.rubyonrails.org/routing.html

I had a few problems working through the Rails Tutorial, and it helped to be able to consult the author's GitHub repo: https://github.com/railstutorial
Find the file you're working on and compare it line by line. Or just cut and paste the full file and see if it will run, then track down your error.

If you check the routes.rb in config you'll probably find that the there is a 'e' missing from /home. Add that and you are golden. Or green. Or whatever :)

check your http:// address, specifically if you are on localhost:3000/path or localhost:3000/path1/path2, etc

Once you change the format of your mapping from get to match you will not need static_pages anymore, just go straight to localhost:3000/pagename e.g localhost:3000/about

From Ruby 2 to Ruby 3 there are some differences, but still the documentation for 3 is not easy to be found. There should be a tutorial only for that: practical differences between rails 2 and 3.
The guide provided by downloading Ruby on rails is "The book of ruby" but it's not good anymore. It should at least contain an advice at the beginning of chapter 19.

In
config/routes.rb
uncomment
root :to => 'welcome#index'

Related

Rails Puma Server: Undefined `extract_multipart' or `before_create' on logout action

I'm unable to logout out of my own page because I get a undefined method Puma error whenever I try. Most of the times it's extract_multipart, but I've also seen before_create.
This is what is shown in the blank page whenever I click logout
Puma caught this error: undefined method `extract_multipart' for Rack::Multipart:Module (NoMethodError)
/var/lib/gems/2.3.0/gems/rack-2.0.1/lib/rack/request.rb:472:in `parse_multipart'
/var/lib/gems/2.3.0/gems/rack-2.0.1/lib/rack/request.rb:335:in `POST'
/var/lib/gems/2.3.0/gems/rack-2.0.1/lib/rack/method_override.rb:39:in `method_override_param'
/var/lib/gems/2.3.0/gems/rack-2.0.1/lib/rack/method_override.rb:27:in `method_override'
/var/lib/gems/2.3.0/gems/rack-2.0.1/lib/rack/method_override.rb:15:in `call'
/var/lib/gems/2.3.0/gems/rack-2.0.1/lib/rack/runtime.rb:22:in `call'
/var/lib/gems/2.3.0/gems/activesupport-5.0.1/lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'
/var/lib/gems/2.3.0/gems/actionpack-5.0.1/lib/action_dispatch/middleware/executor.rb:12:in `call'
/var/lib/gems/2.3.0/gems/actionpack-5.0.1/lib/action_dispatch/middleware/static.rb:136:in `call'
/var/lib/gems/2.3.0/gems/rack-2.0.1/lib/rack/sendfile.rb:111:in `call'
/var/lib/gems/2.3.0/gems/railties-5.0.1/lib/rails/engine.rb:522:in `call'
/var/lib/gems/2.3.0/gems/puma-3.6.2/lib/puma/configuration.rb:225:in `call'
/var/lib/gems/2.3.0/gems/puma-3.6.2/lib/puma/server.rb:578:in `handle_request'
/var/lib/gems/2.3.0/gems/puma-3.6.2/lib/puma/server.rb:415:in `process_client'
/var/lib/gems/2.3.0/gems/puma-3.6.2/lib/puma/server.rb:275:in `block in run'
/var/lib/gems/2.3.0/gems/puma-3.6.2/lib/puma/thread_pool.rb:116:in `block in spawn_thread'
The way I'm managing log-outs is with
<%= link_to(logout_path, method: 'delete', class: 'dropdown-item') do %>
<!-- ... -->
<% end %>
Where the route is defined as delete 'logout' => 'sessions#destroy', and the controller / action is
def destroy
session[:id] = nil
redirect_to '/login'
end
The other error I have only caught once or twice and all I have is a screenshot
Puma: undefined before_create
Any ideas on what might be causing this? Thanks beforehand :)
Edit
As per request, here's the Enumerable concern, which I'm not actually using because I'm also having issues with that (GH issue submitted on auto-inc repository)
require 'autoinc'
module Enumerable
extend ActiveSupport::Concern
included do
include Mongoid::Autoinc
include Mongoid::Document
field :n, as: :number, type: Integer
increments :number
end
end
And the only before_create I'm using is within a completely unrelated module...
Edit 2
Tried updating puma to 3.7.0, didn't fix it. Although it didn't break it further...
Answer obtained from
Delete link sends "Get" instead of "Delete" in Rails 3 view
I needed to add the <%= csrf_meta_tag %> to my head.

Hide ActionController::RoutingError in logs for assets

I am working on a rails app that has a WP home page, and also some images that are being load from WP. On localhost we don't have access to WP content that leads to having a lot of routing errors in logs, in example:
Started GET "/wp-content/uploads/2014/03/facebook-icon1.png" for 127.0.0.1 at 2015-11-20 15:10:48 +0200
ActionController::RoutingError (No route matches [GET] "/wp-content/uploads/2014/03/facebook-icon1.png"):
actionpack (4.2.5) lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'
actionpack (4.2.5) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
Considering we have 5 images on the page we end up having 5 routing errors for each request.
How can I hide these type of errors from logs in dev environment?
Had this exact problem. Create a logger.rb file in your initializers folder and add this code:
# spammers were blowing up our logs
# this suppresses routing errors
if Rails.env.production?
class ActionDispatch::DebugExceptions
alias_method :old_log_error, :log_error
def log_error(env, wrapper)
if wrapper.exception.is_a? ActionController::RoutingError
return
else
old_log_error env, wrapper
end
end
end
end
Maybe this silencer gem can help you.
Usage:
In your environment:
require 'silencer/logger'
config.middleware.swap Rails::Rack::Logger, Silencer::Logger, :silence => [%r{^/wp-content/}]

404 when sending JSON to RoR with curl

I'm very new to RoR, and I'm learning on the fly. With that being said, I'm trying to parse a POST request sent from Formstack to my script. I can view and run the script by going to it's URL and it works just fine, put when I point the Webhook in Formstack to the URL, nothing happens. I ran the below curl request from my command line, and it returned a 404 error.
curl -H "Content-Type: application/json" -d '{"FormID":"1234","UniqueID":"1234","Name":{"first":"John","last":"Smith"},"Email":"test#gmail.com","Phone":"(555) 555-555","Company":"Test Company"}' -X POST "http://scripturl.com"
I checked the data being sent with requestb.in, and everything is being sent properly. So why would it return a 404 error when JSON data is being sent, but run just fine when I view it in my browser? My sample code is below.
firstName = params[:Name]["first"]
lastName = params[:Name]["last"]
email = params["Email"]
company = params["Company"]
phone = params["Phone"]
client.create('contact', firstname: firstName, lastname: lastName, emailaddress1: email, subject: "Test", companyname: company, mobilephone: phone)
Here is the contents of log/production.log:
rack (1.6.4) lib/rack/sendfile.rb:113:in `call'
railties (4.2.3) lib/rails/engine.rb:518:in `call'
railties (4.2.3) lib/rails/application.rb:165:in `call'
passenger (5.0.13) lib/phusion_passenger/rack/thread_handler_extension.rb:94:in `process_request'
passenger (5.0.13) lib/phusion_passenger/request_handler/thread_handler.rb:157:in `accept_and_process_next_request'
passenger (5.0.13) lib/phusion_passenger/request_handler/thread_handler.rb:110:in `main_loop'
passenger (5.0.13) lib/phusion_passenger/request_handler.rb:415:in `block (3 levels) in start_threads'
passenger (5.0.13) lib/phusion_passenger/utils.rb:111:in `block in create_thread_and_abort_on_exception'
I found the problem. I had to add the following into config/routes.rb:
post '/add' => 'contacts#add'
And then in controllers/application_controller.rb, you need to replace this:
protect_from_forgery with: :exception
With this:
protect_from_forgery with: :null_session

Couldn't find ContentTemplate with 'id'=batch_stats

I have a route that looks like this:
get 'content_templates/batch_stats', to: 'content_templates#batch_stats', as: 'batch_stats'
when I invoke the following helper:
<%= link_to 'Statistics', batch_stats_path %>
I expect to go to the batch_stats action of content_templates controller.
def batch_stats
puts "Are we ever getting here??"
...
Unfortunately the action is never triggered. Rails blows up before it even gets to the controller:
ActiveRecord::RecordNotFound - Couldn't find ContentTemplate with 'id'=batch_stats:
activerecord (4.1.5) lib/active_record/relation/finder_methods.rb:320:in `raise_record_not_found_exception!'
activerecord (4.1.5) lib/active_record/relation/finder_methods.rb:420:in `find_one'
activerecord (4.1.5) lib/active_record/relation/finder_methods.rb:404:in `find_with_ids'
activerecord (4.1.5) lib/active_record/relation/finder_methods.rb:68:in `find'
activerecord (4.1.5) lib/active_record/querying.rb:3:in `find'
cancancan (1.9.2) lib/cancan/model_adapters/abstract_adapter.rb:20:in `find'
cancancan (1.9.2) lib/cancan/controller_resource.rb:116:in `find_resource'
cancancan (1.9.2) lib/cancan/controller_resource.rb:68:in `load_resource_instance'
cancancan (1.9.2) lib/cancan/controller_resource.rb:32:in `load_resource'
cancancan (1.9.2) lib/cancan/controller_resource.rb:25:in `load_and_authorize_resource'
cancancan (1.9.2) lib/cancan/controller_resource.rb:10:in `block in add_before_filter'
...
Why is this happening?
I figured it out.
I had two kinds of routes:
resources :content_templates do
get :show_template, on: :member
end
get 'content_templates/batch_stats', to: 'content_templates#batch_stats', as: 'batch_stats'
I got rid of the second one and just added a collection to first one:
resources :content_templates do
get :show_template, on: :member
get :batch_stats, on: :collection
end
And now it no longer looks for a member id.

NameError in UsersController#show

I am currently working on the Ruby on Rails tutorial by Michael Hartl. I am trying to add a page for each user in my database by creating an HTML with embedded ruby page in the views directory. The code for show.html.erb is below:
<%= #user.name %>, <%= #user.email %>
When I add the user to the user_controller.rb file, it looks like this:
class UsersController < ApplicationController
def show
#user = User.find(params[:id])
end
def new
end
end
When I run the rails server and click on open up the users/1 URL, I get a NameError complaining about an uninitialized constant. The error and trace is below:
NameError in UsersController#show
uninitialized constant UsersController::User
Rails.root: /usr/sample_app
Application Trace | Framework Trace | Full Trace
app/controllers/users_controller.rb:3:in `show'
actionpack (3.2.12) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
actionpack (3.2.12) lib/abstract_controller/base.rb:167:in `process_action'
actionpack (3.2.12) lib/action_controller/metal/rendering.rb:10:in `process_action'
actionpack (3.2.12) lib/abstract_controller/callbacks.rb:18:in `block in process_action'
activesupport (3.2.12) lib/active_support/callbacks.rb:414:in
.
.
.
.
Please let me know how to go about this because I cannot pass my spec tests with this error. If anyone has any suggestions or insight I would greatly appreciate them.Thank you.
Controller file name should be users_controller.rb
As you mentioned in comment you don't have any User model defined. To create model run rails g model user name:string email:string. This will create User model with attributes name and email.
Try Rails scaffolding and see what files it creates(in controller, model and views ignore other files for now) and see the contents of those files. To use scaffold for creating User is as below:
rails g scaffold user name:string email:string
rake db:migrate

Resources