New to all coding worlds and new to RoR. I have been doing a tutorial and ran into some issues last night. A friend of mine helped with a few of them. I asked a few here. And yet this one baffles me. I cannot find the issue of it.
The page loads, it shows what needs to be shown. But when I press a link on there it results in this:
Routing Error
No route matches "/index"
So I did rake routes and I got this
Dennis-Buizerts-MacBook-Pro:gpoff dennisbuizert$ rake routes
site_index GET /site/index(.:format) {:controller=>"site", :action=>"index"}
site_about GET /site/about(.:format) {:controller=>"site", :action=>"about"}
site_help GET /site/help(.:format) {:controller=>"site", :action=>"help"}
root /(.:format) {:controller=>"Site", :action=>"index"}
This is in my routes.rb
root :to => "Site#index"
get "site/index"
get "site/about"
get "site/help"
I tried adding map.connects and match but that didn't seem to resolve it.
And my development.log says the follow:
Started GET "/" for 127.0.0.1 at 2011-08-28 10:05:51 +0200
DEPRECATION WARNING: Disabling sessions for a single controller has been deprecated. Sessions are now lazy loaded. So if you don't access them, consider them off. You can still modify the session cookie options with request.session_options. (called from <class:ApplicationController> at /Users/dennisbuizert/Sites/gpoff/app/controllers/application_controller.rb:3)
Processing by SiteController#index as HTML
Rendered site/index.html.erb within layouts/application (1.6ms)
Completed 200 OK in 5ms (Views: 4.3ms | ActiveRecord: 0.0ms)
Started GET "/index" for 127.0.0.1 at 2011-08-28 10:05:52 +0200
ActionController::RoutingError (No route matches "/index"):
Rendered /Users/dennisbuizert/.rvm/gems/ruby-1.9.2-p290/gems/actionpack-3.0.10/lib/action_dispatch/middleware/templates/rescues/routing_error.erb within rescues/layout (0.8ms)
Try
get 'index' => "site#index"
instead of
get "site/index"
Related
thanks in advance for your help!!
I have in routes.rb:
get 'api/streets:name' => 'streets#get_by_name', as: "get_by_name"
I have in streets_controller.rb:
ids = params[:name]
I have in Javascript:
const params = encodeURI('name[]=1&name[]=2')
fetch(`/api/streets?${params}`)
When I call the api from the front end, I get the following log message:
Started GET "/api/streets?name%5B%5D=2&name%5B%5D=5" for 127.0.0.1 at 2019-09-06 17:10:59 -0700
Rendering pages/index.html.erb within layouts/application
Processing by PagesController#index as */*
Parameters: {"name"=>["2", "5"], "path"=>"api/streets"}
Rendered pages/index.html.erb within layouts/application (7.1ms)
Rendering pages/index.html.erb within layouts/application
Why is it using the PagesController and not the StreetsController?
In all other cases where I'm getting and posting and putting and deleting on the api, the router knows what controller to use. It's just this one case when I'm using array parameters where it's routing to the wrong controller.
It's probably just something dumb I've done.
Your route expects a path like: /api/streets123 - where params[:name] would be equal to "123" and no, that's not a typo.
You should just use:
get 'api/streets' => 'streets#get_by_name', as: "get_by_name"
If you need to enforce the existence of the :name parameter then you should use the :constraints option.
For some reason both these URLS are routing to the same file when they shouldn't be, another thing that I noticed when typing in an invalid url such as localhost:3000/topics/inexjojvnsjg it just stays on the same page.
here is what my rails console is telling me when I try to access the url
localhost:3000/topics/index
Started GET "/topics/index" for ::1 at 2015-02-06 17:33:07 -0700
Processing by TopicsController#show as HTML
Parameters: {"id"=>"index"}
Rendered topics/show.html.erb within layouts/application (0.1ms)
User Load (0.8ms) SELECT "users".* FROM "users" WHERE "users"."id" =$1 ORDER BY "users"."id" ASC LIMIT 1 [["id", 1]]
Completed 200 OK in 98ms (Views: 96.5ms | ActiveRecord: 0.8ms)
here is my routes file....
Rails.application.routes.draw do
devise_for :users
get 'welcome/index'
get 'welcome/about'
# get "topics/index"
# get "topics/show"
# get "topics/new"
# get "topics/edit"
#for some reason, using resources:topics, index and show both route to show
resources :topics
root to: 'welcome#index'
post :incoming, to: 'incoming#create'
end
Here is the key info:
Started GET "/topics/index" for ::1 at 2015-02-06 17:33:07 -0700
Processing by TopicsController#show as HTML
Parameters: {"id"=>"index"}
The :index url for a TopicsController is "/topics".
The :show url for a TopicsController is "/topics/:id" or "/topics/1", where the last part of the url gets associated to the params[:id]. With the url "/topics/1" the :id = 1.
So when you go to the url "/topics/index" you are going to the :show action because of the "index" part of the url. You are just setting the :id to "index" instead of a Integer :id. You can see that in the output you pasted here:
Parameters: {"id"=>"index"}
TLDR: "/topics/index" is a route the will pass the Rails router but is an invalid route, because the :id is a String "index".
After installing Rails 4.0.2 from 4.0.0, my tests are failing. It works in the browser (development). This also happens with Rails 4.0.3.
20) Error:
UserFlowsTest#test_login_and_browse_site:
ActionView::Template::Error: No route matches {:id=>nil} missing required keys: [:id]
app/views/posts/_post.haml:10:in `_app_views_posts__post_haml__439576154_44475312'
app/views/posts/index.haml:1:in `_app_views_posts_index_haml__194952016_42560292'
app/controllers/posts_controller.rb:22:in `block (2 levels) in index'
app/controllers/posts_controller.rb:21:in `index'
test/integration/user_flows_test.rb:14:in `block in <class:UserFlowsTest>'
Here is line user_flows_test:14
post_via_redirect "/users/login", username: users(:chloe).username, password: 'passpasspass'
Here is line _post.haml:10
%a{href: user_path(post.user)}= post.user.username if post.user
The documentation is little help: http://api.rubyonrails.org/classes/ActionDispatch/Integration/RequestHelpers.html#method-i-post_via_redirect
http://guides.rubyonrails.org/testing.html#helpers-available-for-integration-tests
Test output:
-----------------------------------------
UserFlowsTest: test_login_and_browse_site
-----------------------------------------
Started GET "/users/login" for 127.0.0.1 at 2014-02-14 21:35:13 -0500
Processing by UsersController#login as HTML
Rendered users/_login.haml (0.0ms)
Rendered users/_new.haml (41.0ms)
Rendered layouts/_header.haml (0.0ms)
Rendered layouts/_right_bar.haml (1.0ms)
Completed 200 OK in 47ms (Views: 47.0ms | ActiveRecord: 0.0ms)
Started POST "/users/login" for 127.0.0.1 at 2014-02-14 21:35:13 -0500
Processing by UsersController#loginCreate as HTML
Parameters: {"username"=>"chloe", "password"=>"[FILTERED]"}
"*********************************************************************** 1"
Redirected to https://www.example.com/
Completed 302 Found in 160ms (ActiveRecord: 0.0ms)
Started GET "/" for 127.0.0.1 at 2014-02-14 21:35:13 -0500
Processing by PostsController#index as HTML
Rendered posts/_vote.haml (7.0ms)
Rendered posts/_post.haml (18.0ms)
Completed 500 Internal Server Error in 45ms
E-------------------------------
This is the end of UsersController#loginCreate
session[:user_id] = #user.id
p "*********************************************************************** #{session[:user_id]}"
redirect_to root_url, :notice => "Logged in! Last seen from #{lastIP}."
This is from the Rails console
irb(main):009:0> p = Post.first
irb(main):011:0> app.user_path(p.user)
=> "/users/1"
Here is the simplest error
21) Error:
PostsControllerTest#test_should_get_index:
ActionView::Template::Error: No route matches {:id=>nil} missing required keys: [:id]
app/views/posts/_post.haml:10:in `_app_views_posts__post_haml__1028637779_43998912'
app/views/posts/index.haml:1:in `_app_views_posts_index_haml__48584214_43731948'
app/controllers/posts_controller.rb:22:in `block (2 levels) in index'
app/controllers/posts_controller.rb:21:in `index'
test/controllers/posts_controller_test.rb:26:in `block in <class:PostsControllerTest>'
So simple...
test "should get index" do
get :index ###### LINE 26
assert_response :success
assert_not_nil assigns(:posts)
end
More info
$ RAILS_ENV=test rails console
DL is deprecated, please use Fiddle
Loading test environment (Rails 4.0.3)
irb(main):001:0> Post.pluck(:user_id)
=> [0, 1, 2]
Maybe your user has not id:
post.user.id => id
This would mean post user is not persisted?
I had to change a user id from 0 to 1 in the post fixtures. That set of a chain reactions of changes. I also put the if on a different line in the HAML instead of at the end of the line, just in case. The HAML properties seem to be evaluated before the if statement.
Your test is testing a collection route, :index, but the test is showing an error on a member route, as indicated by it looking for an :id.
You're either instantiating your partial (app/views/posts/_post.haml) that has a link_to in it with a nil id or your routes file is the problem.
I have a very similar issue to this question: My route returns a blank view (no html when I do view source)
When I access a route, it just shows a blank view with no html when viewing source.
My route returns a blank view (no html when I do view source)
config/routes.rb
SampleApp::Application.routes.draw do
devise_for :users
root to: 'home#index'
end
app/controllers/home_controller.rb
class HomeController < ApplicationController
def index
end
end
app/views/home/index.html.slim (slim is like erb, I tested both out but still no html rendered)
h1 get ready to party!
log/development.log
Started GET "/" for 127.0.0.1 at 2012-12-05 00:36:21 -0500
Processing by HomeController#index as */*
Rendered home/index.html.slim within layouts/application (0.4ms)
Completed 200 OK in 6ms (Views: 5.9ms | ActiveRecord: 0.0ms)
The other question suggested changing ports. I did all the basic stuff like restart server, restart computer, try different ports.
It sounds like your layout could be blank and is not rendering the index view.
The default layout is application.html.erb and should render your views with <%= yield %>.
Also - have you tried restarting your server for kicks? That's the answer to the other SO question you posted so I'm assuming you've tried that...
I'm writing an app in Rails (v3.0.5), which I'm deploying to Heroku.
When I visit http://localhost:3000/places/new in my development environment, I'm taken to the appropriate page (the form for creating a new place). Everything works as expected (I can create new places).
When I try to visit the corresponding page (http://example.heroku.com/places/new) in my Heroku production environment, I'm routed back to the home page for my app.
The contents of my routes.db file:
ExampleSite::Application.routes.draw do
resources :users
resources :sessions, :only => [:new, :create, :destroy]
resources :places
root :to => 'pages#home'
match '/contact', :to -> 'pages#contact'
match '/about', :to -> 'pages#about'
match '/signin', :to -> 'sessions#new'
match '/signout', :to -> 'sessions#destroy'
end
What might be a cause for the discrepancy between development and production?
Note: the only actions I've built out so far in my 'places' controller are 'new' and 'create' (both of which perform as expected in the development environment). Not sure that should be relevant, but keep it in mind. Also, all of the 'users' actions and routes seem to be working as expected in both development and production.
EDIT: As noted in the comment below, /places/new is an authentication-protected page, but in both cases I'm trying this while logged in. Also, when I do try to access /places/new in my production environment while not logged in, the appropriate redirect (to my /signin page) works as expected.
My Heroku log from an attempt at getting /place/new:
2011-05-12T23:37:30+00:00 app[web.1]: Started GET "/places/new" for 74.87.126.82 at Thu May 12 16:37:30 -0700 2011
2011-05-12T23:37:30+00:00 app[web.1]: Processing by PlacesController#new as HTML
2011-05-12T23:37:30+00:00 app[web.1]: Redirected to http://example.heroku.com/
2011-05-12T23:37:30+00:00 app[web.1]: Completed 302 Found in 4ms
2011-05-12T23:37:30+00:00 heroku[router]: GET example.heroku.com/places/new dyno=web.1 queue=0 wait=0ms service=9ms bytes=631
2011-05-12T23:37:30+00:00 app[web.1]:
2011-05-12T23:37:30+00:00 app[web.1]:
2011-05-12T23:37:30+00:00 app[web.1]: Started GET "/" for 74.87.126.82 at Thu May 12 16:37:30 -0700 2011
2011-05-12T23:37:30+00:00 app[web.1]: Processing by PagesController#home as HTML
2011-05-12T23:37:30+00:00 app[web.1]: Rendered layouts/_header.html.erb (4.3ms)
2011-05-12T23:37:30+00:00 app[web.1]: Rendered pages/home.html.erb within layouts/application (5.7ms)
2011-05-12T23:37:30+00:00 app[web.1]: Completed 200 OK in 7ms (Views: 3.5ms | ActiveRecord: 5.8ms)
2011-05-12T23:37:30+00:00 heroku[router]: GET example.heroku.com/ dyno=web.1 queue=0 wait=0ms service=14ms bytes=2357
As it turns out, I had a second (forgotten, since I changed the scope of 'users') authentication condition which checked for admin-level status. THAT condition redirected to the home page if the current user was not an admin.
The test user I created in my development environment IS an admin (since I created that user before switching the scope), while the test user I created in the production environment IS NOT an admin (hence the redirect).
Thanks all for your help! While this mistake was an oversight, your suggestions taught me about a few new diagnostic tools I hadn't thought to use.