I can't catch where a problem is.
I run on server
rails generate scaffold Place name:string lat:numeric lng:numeric
Next, I run
rake db:migrate
So, I try to create new Place on web browser and go to
/places/new
But receive:
ActionController::RoutingError (No route matches [GET] "/place/new"):
I confused because everything works fine on my local machine. What's wrong with me?
btw, routes.rb looks like:
Rails.application.routes.draw do
resources :places
end
You're saying you're navigating to places/new, but the log entry you've included shows that you're actually trying to navigate to place/new, which is not a valid route in this case. Use the plural for the resource.
numeric is not a datatype. You have to use float or integer for lat/long instead of numeric.
Corrected command:
rails generate scaffold Place name:string lat:integer lng:integer
Related
I'm in the process of creating a rails API for scheduling appointments. I'm worrying about making a generic app version first, and then I'm going to make it in to an API, as I havn't done that before.
I've been generating 4-5 scaffolds (rails generate scaffold _____ title:string description: text)
THEN running rake dbmigrate.
When I go to view the file on my local host, while running my rails server I get this error: (unfortunately I can't post images yet with my rep)
No route matches [GET] "/c4cc2"
Rails.root: /Users/Jack/Desktop/Project/CareCloudAttempt2/C4CC2
Application Trace | Framework Trace | Full Trace
Routes
Routes match in priority from top to bottom
Helper HTTP Verb Path Controller#Action
Path / Url
end_times_path GET /end_times(.:format) end_times#index
POST /end_times(.:format) end_times#create
new_end_time_path GET /end_times/new(.:format) end_times#new
edit_end_time_path GET /end_times/:id/edit(.:format) end_times#edit
end_time_path GET /end_times/:id(.:format) end_times#show
PATCH /end_times/:id(.:format) end_times#update
PUT /end_times/:id(.:format) end_times#update
DELETE /end_times/:id(.:format) end_times#destroy
start_times_path GET /start_times(.:format) start_times#index
POST /start_times(.:format) start_times#create
I've also tried entering the name of the routes after my URL
Here are my routes:
```
Rails.application.routes.draw do
resources :end_times
resources :start_times
resources :comments
resources :last_names
resources :first_names
end
```
I was wondering if maybe I needed to run rake db:migrate after each time I scaffold, over if it was another issue.
Thanks!
Migrating after couple of scaffolds is fine, no need to worry there.
What is c4cc2 supposed to be there? Rails looks for resource with that name in routes but isn't finding any. What are you trying to do with that?
This is not necessary you have run rake db:migrate after every scaffold, but you should run rake db:migrate before perform anything with rails server. If you have pending migration you may not browse your application.
But there is no problem with running rake db:migrate after every scaffold.
After adding three custom member routes, everything works fine in the development and production environments, but fails in the testing environment.
In config/routes.rb, our custom member routes are copy, download and abort:
resources :kw_researches do
member do
get 'copy'
get 'download'
put 'abort'
end
end
Running rake routes shows the member routes are all fine and dandy (not a big surprise, as they actually work in production and development):
$ rake routes
copy_kw_research GET /kw_researches/:id/copy(.:format) kw_researches#copy
download_kw_research GET /kw_researches/:id/download(.:format) kw_researches#download
abort_kw_research PUT /kw_researches/:id/abort(.:format) kw_researches#abort
kw_researches GET /kw_researches(.:format) kw_researches#index
POST /kw_researches(.:format) kw_researches#create
new_kw_research GET /kw_researches/new(.:format) kw_researches#new
edit_kw_research GET /kw_researches/:id/edit(.:format) kw_researches#edit
kw_research GET /kw_researches/:id(.:format) kw_researches#show
PUT /kw_researches/:id(.:format) kw_researches#update
DELETE /kw_researches/:id(.:format) kw_researches#destroy
But the tests in both ./spec/views/kw_researches/index.html.erb_spec.rb and ./spec/integration/kw_research_index_page_spec.rb fail with errors such as the following:
10) KwResearch index page KwResearch has all relevant actions
Failure/Error: visit kw_researches_path
ActionView::Template::Error:
undefined method `copy_kw_research_path' for #<#<Class:0x007faab8c9a238>:0x007faab717fd40>
Why is copy_kw_research_path not available, while its good (standard helper) friend edit_kw_research_path is? Thanks...
When we returned to the problem two weeks later, it just disappeared: It would appear that restarting both the rails server (thin) and guard solved it.
I am a newbie in Rails and I get routing errors like this:
No route matches [GET] "/static_pages/home"
I know it's because I am missing some codes in routes.rb, how do I get these routes?
Thank you
Mori's answer is spot on. But as a beginner, you will find the following command to be your friend:
rake routes
Good luck
rake routes is very slow (30s in my computer) but I need it for routing spec.
So, is there a way to get all routes like rake routes? (or how rake routes works?)
I use Rails 3 and all I have seen is for Rails 3, and I found nothing I can use in the rails doc.
Rails.application.routes.routes.to_a
.to_a is optional, it just converts it to an array.
(I found this line in railties/lib/rails/tasks/routes.rake)
I use it like :
routes[10].defaults => {:action=>"edit", :controller=>"polls"}
Edit : You can find the (quite hacky) way I do my routing specs here : https://gist.github.com/52ac6d848ce0d9fd52ac
If you're using RSpec, you can use routing specs in your tests.
Another option is the rake shell; I love it.
After following a tutorial Ive found. Im now redoing it again, without the scaffolding part, to learn it better.
However, editing my \app\views\home\index.html.erb to contain:
<h1>Rails test project</h1>
<%= link_to "my blog", posts_path>
I get an error:
undefined local variable or method `posts_path' for #<ActionView::Base:0x4e1d954>
Before I did this, I ran rake db:create, defined a migration class and ran rake db:migrate, everything without a problem.
So the database should contain a posts table. But that link_to command cant seem to find posts_path. That variable (or is it even a function?) is probably defined through the scaffold routine.
My question now is; how do I do that manually myself, define posts_path?
You will need to define a path to your posts in config/routes.rb
Rails 2.x syntax:
map.resources :posts
Rails 3.x syntax:
resources :posts
The _path methods are dynamically generated typically. The method missing error comes about when there isn't a route to the object specified or in this case the method you're calling explicitly.
Defining a route should fix this. HermanD above showed one way to do this.
You can run 'rake routes' from the root of your rails app to see all the routes that are configured
<%= link_to "my blog", posts_path>
If this is exactly what your erb contained, it's missing the percent sign at the end of the scriptlet element. Not sure if that caused your problem, or maybe I'm taking things too literally....