rspec chokes on named path in rails 3 - ruby-on-rails

Re-writing a photography portfolio site, and urls have to follow the format:
[site].com/portfolio/[category]/[image]
to match current implementation.
[site].com and [site].com/portfolio should resolve to the root. my routes file is below:
root :to => 'portfolio#index'
match '/portfolio', :to => 'portfolio#index'
match '/portfolio/*category/*photo', :to => 'portfolio#photo'
match '/portfolio/*category', :to => 'portfolio#photo'
I added a link in my header to:
%li= link_to "Portfolio", root_path
and this works fine, but when I add:
%li= link_to "Editorial", portfolio_photo_path
my pages resolve fine in-browser, but rspec chokes out on me; even the simplest http success tests that would previously run fine now return:
1) PortfolioController GET 'index' returns http success
Failure/Error: get 'index'
ActionView::Template::Error:
undefined local variable or method `portfolio_photo_path' for #<#<Class:0x572d210>:0x5733d68>
# ./app/views/layouts/_header.html.haml:7:in `_app_views_layouts__header_html_haml__420220390_28357236'
# ./app/views/layouts/application.html.haml:10:in `_app_views_layouts_application_html_haml__156489427_30065868'
# ./spec/controllers/portfolio_controller_spec.rb:8:in `block (3 levels) in <top (required)>'
(when I rake routes, i get the following)
$ rake routes
root / portfolio#index
portfolio /portfolio(.:format) portfolio#index
/portfolio/*category/*photo(.:format) portfolio#photo
/portfolio/*category(.:format) portfolio#photo

You should add ":as => :portfolio" statement:
match '/portfolio', :to => 'portfolio#index', :as => :portfolio

Answered in my comment above; I'd defined variable-globbed routes, and brain-fartedly didn't realize that i'd have to construct the links directly and dynamically.

Related

RoR, Sizzle Uncaught Error: Syntax error, unrecognized expression: /sv/priser

The error
jquery.self-977e28a4e7fded7698789e394a585c6339d54c0ad1537f498a40d2800098a521.js?body=1:1464 Uncaught Error: Syntax error, unrecognized expression: /sv/priser
at Function.Sizzle.error (jquery.self-977e28a4e7fded7698789e394a585c6339d54c0ad1537f498a40d2800098a521.js?body=1:1464)
at Sizzle.tokenize (jquery.self-977e28a4e7fded7698789e394a585c6339d54c0ad1537f498a40d2800098a521.js?body=1:2121)
at Sizzle.select (jquery.self-977e28a4e7fded7698789e394a585c6339d54c0ad1537f498a40d2800098a521.js?body=1:2542)
at Function.Sizzle [as find] (jquery.self-977e28a4e7fded7698789e394a585c6339d54c0ad1537f498a40d2800098a521.js?body=1:865)
at jQuery.fn.init.find (jquery.self-977e28a4e7fded7698789e394a585c6339d54c0ad1537f498a40d2800098a521.js?body=1:2788)
at new jQuery.fn.init (jquery.self-977e28a4e7fded7698789e394a585c6339d54c0ad1537f498a40d2800098a521.js?body=1:2905)
at jQuery (jquery.self-977e28a4e7fded7698789e394a585c6339d54c0ad1537f498a40d2800098a521.js?body=1:76)
at HTMLAnchorElement.<anonymous> (kontakt:50)
at HTMLAnchorElement.dispatch (jquery.self-977e28a4e7fded7698789e394a585c6339d54c0ad1537f498a40d2800098a521.js?body=1:4733)
at HTMLAnchorElement.elemData.handle (jquery.self-977e28a4e7fded7698789e394a585c6339d54c0ad1537f498a40d2800098a521.js?body=1:4545)
Getting this error when clicking on a to_link tag in RoR v.5.2 (Ruby v2.5)
The link works, and takes me to the targeted html.erb file. I can't find any solution that has worked.
The tag the error occurs for all tags like this, this one is just an example.
<%= link_to 'Prices', en_prices_path, :class => "page-scroll", id: 'prices' %>
My routes
Rails.application.routes.draw do
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
namespace :en do
get 'index' => 'pages#index', as: 'home'
get 'prices' => 'pages#prices'
get 'about' => 'pages#about'
get 'contact' => 'pages#contact'
end
namespace :sv do
root 'pages#index', as: 'home'
get 'priser' => 'pages#priser'
get 'om' => 'pages#om'
get 'kontakt' => 'pages#kontakt'
end
get '*path' => redirect('/sv')
end
Its noteworthy to mention that the link exists in a partial _nav file that that is being rendered into an index or contact.html.erb file amongst others, that is then being rendered into the application.html.erb file.
I hear talk about jquery complaining about bad selectors, which makes me believe the paths (for example sv_priser_path) the router gives me is bad (it gives me /sv/priser). Can i edit them perhaps? Would that stop the error from happening? I tried parsing them with
$($.parseHTML(sv_priser_path)[1]
but that i didnt get that to work either. Didnt get a replace script to work either to remove the slash.

Running minitest controller get ActionController::UrlGenerationError: No route matches

I know, that with this topic more questions asket, but i don't found what i need.
Currently i'm updating rails app from 3.2.13 to 4.2.0 and after upgrading rails naturally fails tests. These tests are passed in 3.2.13
So, i have this route:
get '/catalogs/:article_id/get_applicability_by_brand/:brand_id', :to => 'catalogs#get_applicability_by_brand', constrains: { format: 'js' }, as: :catalog_get_applicability_by_brand
Result of rake routes like this:
catalog_get_applicability_by_brand GET /catalogs/:article_id/get_applicability_by_brand/:brand_id(.:format) catalogs#get_applicability_by_brand {:constrains=>{:format=>"js"}}
Controller action, it only render js.erb template:
def get_applicability_by_brand
#applicability = CatalogAccess::TecDoc.get_applicability_by_brand(params[:article_id], params[:brand_id])
end
Minitest controller test:
def test_get_applicability_by_brand_action
expected_applicability = [
{ 'model_name' => 'Model 1',
'name' => 'fake name',
'year_of_construct_from' => '2000',
'year_of_construct_to' => '2010',
'construction_type' => 'fake type' },
{ 'model_name' => 'Model 1',
'name' => 'fake name 2',
'year_of_construct_from' => '1991',
'year_of_construct_to' => '2005',
'construction_type' => 'fake type' }
]
CatalogAccess::TecDoc.expects(:get_applicability_by_brand).with('12', '23').returns expected_applicability
xhr :get, :get_applicability_by_brand, :article_id => '12', :brand_id => '23', :format => "js"
assert_response 200
assert_template 'get_applicability_by_brand'
assert_template :partial => '_tecdoc2_applicability'
end
Test error message is:
ActionController::UrlGenerationError: ActionController::UrlGenerationError: No route matches {:action=>"get_applicability_by_brand", :article_id=>"12", :brand_id=>"23", :controller=>"catalogs", :format=>"js"}
I found that if append to my test option 'use_route', it will be pass, but get warning that seems not good solution
xhr :get, :get_applicability_by_brand, :article_id => '12', :brand_id => '23', :format => "js", :use_route => 'catalogs'
Warning message:
DEPRECATION WARNING: You are trying to generate the URL for a named route called "catalogs" but no such route was found. In the future, this will result in an `ActionController::UrlGenerationError` exception. (called from test_get_applicability_by_brand_action at /home/sdilshod/webapp/ps_base/apps/www/test/controllers/catalogs_controller_test.rb:627)
DEPRECATION WARNING: Passing the `use_route` option in functional tests are deprecated. Support for this option in the `process` method (and the related `get`, `head`, `post`, `patch`, `put` and `delete` helpers) will be removed in the next version without replacement. Functional tests are essentially unit tests for controllers and they should not require knowledge to how the application's routes are configured. Instead, you should explicitly pass the appropiate params to the `process` method. Previously the engines guide also contained an incorrect example that recommended using this option to test an engine's controllers within the dummy application. That recommendation was incorrect and has since been corrected. Instead, you should override the `#routes` variable in the test case with `Foo::Engine.routes`. See the updated engines guide for details. (called from test_get_applicability_by_brand_action at /home/sdilshod/webapp/ps_base/apps/www/test/controllers/catalogs_controller_test.rb:627)
DEPRECATION WARNING: You are trying to generate the URL for a named route called "catalogs" but no such route was found. In the future, this will result in an `ActionController::UrlGenerationError` exception. (called from test_get_applicability_by_brand_action at /home/sdilshod/webapp/ps_base/apps/www/test/controllers/catalogs_controller_test.rb:627)
Advise me please correct solution.
I'll hope your help, thanks!

Rails Engine not routing in webpage

I am trying to mount a rails engine piggybak_paypal.
Inside my application's config/routes.rb file I add
mount PiggybakPaypal::Engine => '/paypal', :as => 'piggybak_paypal'
and the engines route is like this
PiggybakPaypal::Engine.routes.draw do
get "/express" => "paypal#express", :as => :paypal_express
get "/process" => "paypal#process_express", :as => :paypal_process
end
When I try rake routes the route of the engine is showing correctly
Routes for PiggybakPaypal::Engine:
paypal_express GET /express(.:format) piggybak_paypal/paypal#express
paypal_process GET /process(.:format) piggybak_paypal/paypal#process_express
but when I open my website and use /express the server cannot find the route
ActionController::RoutingError (No route matches [GET] "/express"):
I've look around but I can't find a solution to it.
okay I just figure out why the routing is not working and I can't believe I spend hours on this and didn't found out why.
All I did is change the route from
mount PiggybakPaypal::Engine => '/paypal', :as => 'piggybak_paypal'
to
mount PiggybakPaypal::Engine => '/', :as => 'piggybak_paypal'
then /express works.

Rails Routing help

I have the following in my routes.rb:
match "/profile/permissions" => 'profiles#edit_permissions', :as => 'edit_permissions', :via => :get
match "/profile/permissions" => 'profiles#update_permissions', :as => 'update_permissions', :via => :post
Getting /profile/permissions works find, yet, when I post to /profile/permissions, I get:
Started POST "/profile/permissions" for 127.0.0.1 at 2011-03-29 12:12:09 -0400
ActionController::RoutingError (No route matches "/profile/permissions"):
Any ideas?
Can you put your routes.rb in a gist? Sometimes there's a conflict, and extra eyes are needed to find it. Also, for the sake of fidgeting, you might try out the new shorthand:
get "/profile/permissions" => 'profiles#edit_permissions', :as => 'edit_permissions'
post "/profile/permissions" => 'profiles#update_permissions', :as => 'update_permissions'
Unto themselves, both your routing text and this example successfully trigger their actions on the specified controller.
Start a test app, with only a routing file, run its server, and try to post to those URLs. If you want to use the Rails console, check out this.
> require "uri"
> require "net/http"
> url = app.update_permissions_url
> url = "http://localhost/profile/permissions" (if your domain is bollocks)
> Net::HTTP.post_form(URI.parse(url),{})
Easy to minimalize your environment for testing, now.

Basic Rails3 routes question?

My routes.rb has the following 2 lines:
match "/", :to => "main#index"
match "main/index", :to => "main#index"
When I type localhost:3000/main/index in a browser I see the proper page (views/main/index.html.erb).
However, if I type just localhost:3000 I see public/index.html (I expect to see views/main/index.html.erb as well).
What am I missing ?
Remove public/index.html:
rm public/index.html
This is happening because static files (those in public/) are served in priority to the routes.

Resources