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.
Related
I am following along with the Ruby on Rails Tutorial by Michael Hartl and am up to the point of running tests. The test is a simple "supposed to fail" test shown here:
test "should get about" do
get static_pages_about_url
assert_response :success
end
with the error being simply that the about page, controller method, and route do not yet exist. However when I run the test I get a massive block of code which fills up my terminal. This is just a small sampling as an example of what i'm seeing.
E
Error:
StaticPagesControllerTest#test_should_get_about:
NameError: undefined local variable or method `static_pages_about_url' for #<StaticPagesControllerTest:0x00007fe6682cc760 #_routes=nil, #NAME="test_should_get_about", #failures=[#<Minitest::UnexpectedError: Unexpected exception>], #assertions=0, #integration_session=#<#<Class:0x00007fe6685342c8>:0x00007fe66859fc58 #_routes=nil, #app=#<SampleApp::Application:0x000055d7735cea68 #_all_autoload_paths=["/home/clay/Programs/sample_app/app/channels", "/home/clay/Programs/sample_app/app/controllers", "/home/clay/Programs/sample_app/app/controllers/concerns", "/home/clay/Programs/sample_app/app/helpers", "/home/clay/Programs/sample_app/app/jobs", "/home/clay/Programs/sample_app/app/mailers", "/home/clay/Programs/sample_app/app/models", "/home/clay/Programs/sample_app/app/models/concerns"], #_all_load_paths=["/home/clay/Programs/sample_app/lib", "/home/clay/Programs/sample_app/vendor", "/home/clay/Programs/sample_app/app/channels", "/home/clay/Programs/sample_app/app/controllers", "/home/clay/Programs/sample_app/app/controllers/concerns", "/home/clay/Programs/sample_app/app/helpers", "/home/clay/Programs/sample_app/app/jobs", "/home/clay/Programs/sample_app/app/mailers", "/home/clay/Programs/sample_app/app/models", "/home/clay/Programs/sample_app/app/models/concerns"], #app=#<ActionDispatch::HostAuthorization:0x000055d774009820 #app=#<Rack::Sendfile:0x000055d774009988 #app=#<ActionDispatch::Static:0x000055d773fe4f98 #app=#<ActionDispatch::Executor:0x000055d773fe5088 #app=#<ActiveSupport::Cache::Strategy::LocalCache::Middleware:0x000055d77382f4d8 #name="ActiveSupport::Cache::Strategy::LocalCache", #local_cache_key=:active_support_cache_null_store_local_cache_4420, #app=#<Rack::Runtime:0x000055d773fe51f0 #app=#<Rack::MethodOverride:0x000055d773fe5290 #app=#<ActionDispatch::RequestId:0x000055d773fe5330 #app=#<ActionDispatch::RemoteIp:0x000055d773fe5420 #app=#<Rails::Rack::Logger:0x000055d773fe54e8 #app=#<ActionDispatch::ShowExceptions:0x000055d773fe5588 #app=#<ActionDispatch::DebugExceptions:0x000055d773fe56c8 #app=#<ActionDispatch::ActionableExceptions:0x000055d773fe5790 #app=#<ActionDispatch::Reloader:0x000055d773fe58f8 #app=#<ActionDispatch::Callbacks:0x000055d773fe5998 #app=#<ActionDispatch::Cookies:0x000055d773fe5a38 #app=#<ActionDispatch::Session::CookieStore:0x000055d773fe5c68 #app=#<ActionDispatch::ContentSecurityPolicy::Middleware:0x000055d773fe5d58 #app=#<ActionDispatch::PermissionsPolicy::Middleware:0x000055d773fe5e20 #app=#<Rack::Head:0x000055d773fe5ec0 #app=#<Rack::ConditionalGet:0x000055d773fe5f60 #app=#<Rack::ETag:0x000055d773fe6050 #app=#<Rack::TempfileReaper:0x000055d773fe60f0
All I really need is the NameError: undefined local variable or method 'static_pages_about_url' and the location of the failing test right? Is this standard for failing tests in Ruby on Rails or is there a way to change how error messages are displayed from showing all of this less useful information?
In test driven development (TDD), you write the tests first. When you run them, all of them will be failing because you have not written the implementation yet (this is your "Red"). After that, you write the implementation that makes the tests pass (this is your "Green"). Once all tests are passsing, it comes the stage where you refactor the implementation (if required, this is your "Refactor"). Red, Green, Refactor.
In your particular example, you have written the test that checks if you can "GET" the about page but because you have not yet implemented the solution, your tests are failing.
In order to make the test pass, you'll need to add this to your route.rb file.
get 'static_pages/about'
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
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.
Weel, im upgrading my application of Rails 3.2 to 4.0 today, and use Ruby 2.0, but after upgrade and adjust the things when i try enter im my website i get error
undefined method `product_url' for #<#<Class:0x007fa5ae0111d8>:0x007fa5abdd4a20>
im my routes have the product path here:
resources :products,:only => [:show,:index] do
post :calc_ship
end
and when i execute rake routes
product_pt_br GET /produtos/:id(.:format) products#show {:locale=>"pt-BR"}
product_en GET /en/products/:id(.:format) products#show {:locale=>"en"}
Before update running normal, i dont know what is this, seems very simple but I could not solve, please help me i thanks much
Well, the output of rake routes shows you that the name of the product_url method should be product_pt_br_url or product_en_url.
**product_pt_br** GET /produtos/:id(.:format)
**product_en** GET /en/products/:id(.:format)
I'm trying to implement Swiftype on Rails 3.2.5. However, when adding a category, and running rake jobs:work, I get the following error message:
CreateSwiftypeDocumentJob failed with NoMethodError: undefined method
`post_url' for #
In the CreateSwiftypeDocumentJob model I have :
post = ProposalCategory.find(proposal_category_id)
url = Rails.application.routes.url_helpers.post_url(post)
If somebody could please help with this, that'd be much appreciated.
post_url should only be used if you actually have a route for the Post resource.
What is in your routes.rb file? Presumably something like:
resources :proposal_categories
In that case, you would need to use a route like proposal_category_url.