I can't get this to pass:
%w[home help about contact].each do |page|
get :controller => 'static_pages', :action => page
end
I was trying to refactor this code:
get 'static_pages/home'
get 'static_pages/help'
get 'static_pages/about'
get 'static_pages/contact'
Please help.
static_pages = %w(home help about contact).map {|p| p.to_sym}
resources :static_pages, only: static_pages do
static_pages.each do |page|
get page, on: :collection
end
end
render
$ rake routes
home_static_pages GET /static_pages/home(.:format) static_pages#home
help_static_pages GET /static_pages/help(.:format) static_pages#help
about_static_pages GET /static_pages/about(.:format) static_pages#about
contact_static_pages GET /static_pages/contact(.:format) static_pages#contact
use only: static_pages to not generate CRUD routes.
I guess if you don't like the original code of:
root to: 'static_pages#home'
match '/help', to: 'static_pages#help'
match '/about', to: 'static_pages#about'
match '/contact', to: 'static_pages#contact'
and you want to get the same routes, you could go with a simple loop like:
root to: 'static_pages#home'
%w(help about contact).each do |page|
get "#{page}" => "static_pages##{page}"
end
Though, to me this doesn't really seem like enough of a refactor to justify it.
Edit
To fix the errors on your code, use:
%w(home help about contact).each do |page|
get "/#{page}", :controller => 'static_pages', :action => page
end
Check the api for match for valid arguments to get.
Related
A url helper I am using does not work, even if it shows at rake routes. Not sure why, hopefully someone could give me a suggestion or point out errors. Something to do with using a token as an id or configuration?
Clicking on this:
<%= link_to "Reset password", edit_password_reset_url(#member.reset_token, email: #member.email) %>
Gives this error message (email parameter does pass through):
No route matches [GET] "/password_resets/$2a$12$Be6H4xiPjqlBtLxAozB7EujVy.2nZSLFJzL3LYDugvDmnn6aoGJ0G/edit"
The :new, :create paths work but not the :edit.
routes.rb (The last line):
Rails.application.routes.draw do
get 'sessions/new'
root 'static_pages#home'
get 'help' => 'static_pages#help'
get 'about' => 'static_pages#about'
get 'contact' => 'static_pages#contact'
get 'signup' => 'members#new'
get 'login' => 'sessions#new'
post 'login' => 'sessions#create'
delete 'logout' => 'sessions#destroy'
resources :members
resources :account_activations, only: [:edit]
resources :password_resets, only: [:new, :create, :edit, :update]
end
This is from the Ruby on Rails Tutorial Third Edition by Michael Hartl (chapter 10)
Thank you for your time reading this and all answers will be appreciated.
The issue here is that -
Your edit_password_reset route is
/password_resets/:id/edit
but while calling it you are not passing the id but calling it on the member token like this -
<%= link_to "Reset password", edit_password_reset_url(#member.reset_token, email: #member.email) %>
So for the above call to work your edit route should be something like
/password_resets/<token>/edit
The new and create paths work because they don't require this token. Try to follow the tutorial properly and you will find where you have gone wrong.
I am missing the contacts#show prefix 'contact' as seen below.
rake routes
contacts GET /contacts(.:format) contacts#index
POST /contacts(.:format) contacts#create
new_contact GET /contacts/new(.:format) contacts#new
edit_contact GET /contacts/:id/edit(.:format) contacts#edit
GET /contacts/:id(.:format) contacts#show
PATCH /contacts/:id(.:format) contacts#update
PUT /contacts/:id(.:format) contacts#update
DELETE /contacts/:id(.:format) contacts#destroy
I am thinking this is the reason why I am getting a dot instead of a slash when clicking the following link.
_contact.html.erb
<%= link_to "delete contact", contact, method: :delete,
data: { confirm: "You sure?" } %>
The server logs the proper DELETE request, however, cannot render /contact.26 instead of the correct /contacts/26.
Started DELETE "/contact.26" for 128.177.12.30 at 2016-04-13 21:04:30 +0000
ActionController::RoutingError (No route matches [DELETE] "/contact.26"):
Every post I've come across with a dot instead of a slash seems to stem from a pluralization error, however, I don't believe this is the case here.
In addition, I've removed resources :contacts from my routes file, run $ rake routes, added resources :contacts, run $ rake routes, and the problem persists.
This problem seems to be unique to the contacts model, as the rest of my models aren't missing any prefixes or having this error when deleting.
How do I add the 'contact' prefix back to 'contacts#show'?
routes.rb file for reference:
Rails.application.routes.draw do
root 'static_pages#home'
get 'help' => 'static_pages#help'
get 'about' => 'static_pages#about'
get 'contact' => 'static_pages#contact'
get 'signup' => 'users#new'
get 'login' => 'sessions#new'
get 'newevent' => 'events#new'
get 'newteam' => 'teams#new'
get 'newperformance' => 'performances#new'
get 'newhotel' => 'hotels#new'
get 'newcontact' => 'contacts#new'
get 'newflight' => 'flights#new'
get 'newground' => 'grounds#new'
get 'newguest' => 'guests#new'
post 'login' => 'sessions#create'
delete 'logout' => 'sessions#destroy'
resources :users
resources :events
resources :teams do
member do
get :events
end
end
resources :performances
resources :hotels
resources :contacts
resources :flights
resources :grounds
resources :guests
resources :account_activations, only: [:edit]
resources :password_resets, only: [:new, :create, :edit, :update]
end
you may have a conflict between the routes set by
resources :contacts
and
get 'contact' => 'static_pages#contact'
The easiest solution is probably to change the static page's links. Best to keep the resources together if you can.
I just came across this same error.
the fix to my issue was that 's' at the end of resources
I had 'resource', and getting the same problem as you.
I changed it to 'resources' and it fixed my problem
in my case was just a typo.
This error showed up for me yesterday. What fixed it was deleting helper files that were not being used. Might be worth a try for you.
I have a problem, I follow this tutorial :https://richonrails.com/articles/google-authentication-in-ruby-on-rails.
I have done everything except rails g controller home show because I have already a static_pages controller and home.html.erb
So in my route.rb I have replace this line from the tutorial:
resource :home, only: [:show]
root to: "home#show"
by
resource :static_pages, only: [:home]
root to: 'static_pages#home'
but when I click on the link after I've launched the server (localhost) I have this error:
No route matches [GET] "/auth/google_oauth2"
Do you have any idea about this problem? Hope you could help me.
Thank you in advance.
EDIT : My route.rb code
Rails.application.routes.draw do
get 'sessions/create'
get 'sessions/destroy'
get 'sessions/new'
get 'users/new'
root 'static_pages#home'
get 'help' => 'static_pages#help'
get 'about' => 'static_pages#about'
get 'contact' => 'static_pages#contact'
get 'signup' => 'users#new'
get 'login' => 'sessions#new'
post 'login' => 'sessions#create'
delete 'logout' => 'sessions#destroy'
resources :users
end
GoogleAuthExample::Application.routes.draw do
get 'auth/:provider/callback', to: 'sessions#create'
get 'auth/failure', to: redirect('/')
get 'signout', to: 'sessions#destroy', as: 'signout'
resources :sessions, only: [:create, :destroy]
resource :static_pages, only: [:home]
root to: 'static_pages#home'
end
It work! I do that with devise. but just dont forget to install "mongoid" gem in order to set up devise (that took me lot of time to determine mogoid was missing).
Thank you all!
I am following Michale Hartl's tutorial and I am currently at this step:
Listing 5.23. Adding a mapping for the root route. config/routes.rb
SampleApp::Application.routes.draw do
root to: 'static_pages#home'
match '/help', to: 'static_pages#help'
match '/about', to: 'static_pages#about'
match '/contact', to: 'static_pages#contact'
end
I have copied his exact coding to my config/routes.rb and continue to get a Routing Error:
Routing Error
No route matches [GET] "/static_pages/home.html".
--
Not sure what to do at this point to get my home routed as the index, and to be able to use root_path to link back to 'Home'.
Remove ../static_pages/.. from the URL you've entered in the browser.
this code is correct
SampleApp::Application.routes.draw do
root :to => 'static_pages#home'
get '/help' => 'static_pages#help'
get '/about' => 'static_pages#about'
get '/contact' => 'static_pages#contact'
end
You do not have to delete the index page, the rspec test in the tutorial is not wrong, this is the updated code with current available methods
describe "Home page" do
it "should have the content 'Sample App'" do
visit '/'
page.should have_content('Sample App')
end
end
This is using ruby -v ruby 2.0.0p47 and rails -v Rails 4.0.3
you can also get a failure if your using before { visit help_path }, using the Rails Console and checking the path app.help_path confirms 'before { visit help_path } is correct but if this code is not in spec/rspec_helpers.rb then the test will fail because it cannot find the path
Named routes should work if you put the following in rspec_helper.rb:
Rspec.configure do |config|
config.include Rails.application.routes.url_helpers
...
end
SampleApp::Application.routes.draw do
root :to => 'static_pages#home'
get '/help' => 'static_pages#help'
get '/about' => 'static_pages#about'
get '/contact' => 'static_pages#contact'
end
this code is right.
You need to delete or rename the public/index.html and then it will work.
I've read official guide but still have misunderstanding.
Is this code able for refactoring?
match '/help', :to => 'home#help'
match '/contact', :to => 'home#contact'
match '/about', :to => 'home#about'
help, contact and about are the only actions in the controller home.
I did this on a hunch and it's not mentioned in the documentation, but it looks like it works (I'm on rails 3.1):
controller :home do
get 'help'
get 'contact'
get 'about'
end
This also creates the help_url, help_path, etc helpers.
One warning though, this restricts the http verbs to GET. If you have a POST action (as an example, for a contact form), you could do either:
controller :home do
get 'help'
match 'contact', :via => [:get, :post]
get 'about'
end
or just:
controller :home do
get 'help'
match 'contact'
get 'about'
end
which will allow all http verbs on the contact route. But I find it better to be explicit about the accepted verbs.
You should be able to use rails shorthand here and do:
match 'home/help'
match 'home/contact'
match 'home/about'
Since the method names match this should work.
you could of course do
match '/:action', :controller => :home, :constraints => { :action => /^(help|contact|about)$/ }
but this is neither prettier, nor really shorter