What generates articles_index_path ?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

What generates articles_index_path ?

Ralph Shnelvar
What generates articles_index_path ? I am porting some Rails 4 code to Rails 5 and I hit the following problem when using  Rspec:  articles_index_path  is undefined.

routes.rb
Rails.application.routes.draw do
 devise_for :users
 root 'static_pages#root'
 resources :users

 resources :articles
 root to: "articles#index"
 # byebug
end


In /home/real-estate-data-mining/spec/requests/article_spec.rb I have
require 'rails_helper'
RSpec.describe "Article", type: :request do
 describe "GET /article" do
   it "works! (now write some real specs)" do
     byebug
     # get article_index_path
     get articles_index_path
     expect(response).to have_http_status(200)
   end
 end
end

When I examine the code environment around the byebug I see
[3, 12] in /home/real-estate-data-mining/spec/requests/article_spec.rb
   3: RSpec.describe "Article", type: :request do
   4:   describe "GET /article" do
   5:     it "works! (now write some real specs)" do
   6:       byebug
   7:       # get article_index_path
=>  8:       get articles_index_path
   9:       expect(response).to have_http_status(200)
  10:     end
  11:   end
  12: end
(byebug) articles_path
"/articles"
(byebug) articles_index_path
*** NameError Exception: undefined local variable or method `articles_index_path' for #<RSpec::ExampleGroups::Article_2::GETArticle:0x00563031a8a0e0>

nil
(byebug) article_index_path
*** NameError Exception: undefined local variable or method `article_index_path' for #<RSpec::ExampleGroups::Article_2::GETArticle:0x00563031a8a0e0>

nil
(byebug)



This must be a common software pattern in Rails but, damn, I have Googled for what feels like a googol (it's not a typo) hours without success trying to find what metaprogramming code would generate articles_index_path .  

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/1403647490.20170710123633%40dos32.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: What generates articles_index_path ?

Ariel Rzezak
Seeing your routes, try root_path or articles_path. 

On Jul 10, 2017, at 3:36 PM, Ralph Shnelvar <[hidden email]> wrote:

What generates articles_index_path ? I am porting some Rails 4 code to Rails 5 and I hit the following problem when using  Rspec:  articles_index_path  is undefined.

routes.rb
Rails.application.routes.draw do
 devise_for :users
 root 'static_pages#root'
 resources :users

 resources :articles
 root to: "articles#index"
 # byebug
end


In /home/real-estate-data-mining/spec/requests/article_spec.rb I have
require 'rails_helper'
RSpec.describe "Article", type: :request do
 describe "GET /article" do
   it "works! (now write some real specs)" do
     byebug
     # get article_index_path
     get articles_index_path
     expect(response).to have_http_status(200)
   end
 end
end

When I examine the code environment around the byebug I see
[3, 12] in /home/real-estate-data-mining/spec/requests/article_spec.rb
   3: RSpec.describe "Article", type: :request do
   4:   describe "GET /article" do
   5:     it "works! (now write some real specs)" do
   6:       byebug
   7:       # get article_index_path
=>  8:       get articles_index_path
   9:       expect(response).to have_http_status(200)
  10:     end
  11:   end
  12: end
(byebug) articles_path
"/articles"
(byebug) articles_index_path
*** NameError Exception: undefined local variable or method `articles_index_path' for #<RSpec::ExampleGroups::Article_2::GETArticle:0x00563031a8a0e0>

nil
(byebug) article_index_path
*** NameError Exception: undefined local variable or method `article_index_path' for #<RSpec::ExampleGroups::Article_2::GETArticle:0x00563031a8a0e0>

nil
(byebug)



This must be a common software pattern in Rails but, damn, I have Googled for what feels like a googol (it's not a typo) hours without success trying to find what metaprogramming code would generate articles_index_path .  

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/1403647490.20170710123633%40dos32.com.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/39D48C3D-6368-4B3B-80A4-2F0A5FFE10A1%40gmail.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: What generates articles_index_path ?

Ralph Shnelvar
In reply to this post by Ralph Shnelvar
I'm going to try to answer my own question.

I tracked down this answer:
https://stackoverflow.com/questions/23819312/rails-undefined-method-user-index-pathEnter code here.

The most important sentence tompave wrote in https://stackoverflow.com/questions/23819312/rails-undefined-method-user-index-path is this:

A model with a plural name, e.g. class Users, will confuse rails.


Analysis:


I was following along on
https://code.tutsplus.com/tutorials/authorization-with-pundit--cms-28202

And one can find there
rails generate scaffold Articles title:string body:text

This generates a warning

[WARNING] The model name 'Articles' was recognized as a plural, using the singular 'Article' instead. Override with --force-plural or setup custom inflection rules for this noun before running the generator.

The second most important sentence in tompave's answer is
Normally it would be user_path for singular routes (show, update, delete) and users_path for the plural ones (index, create). Here, however, users_path must be used for the singular routes, and Rails will fallback to use users_index_path for index and create.

The person who coded up the Rspec test seems to have known using the plural form "Articles" would generate the articles_index_path method rather than article_path.

And thus my confusion has been addressed.

I wish I could thank tompave directly

 



--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/7af92c34-b723-4839-ad0b-8b34c483de9e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Loading...