Thanks in advance for any help.
I'm working my way through Michael Hartl's "learn enough" RoR lessons and am at the point where we're working with authenticating users via sessions.
Per the lesson, I've just run rails generate controller Sessions new and nothing blew chunks:
$ rails generate controller Sessions new
Running via Spring preloader in process 4407
create app/controllers/sessions_controller.rb
route get 'sessions/new'
invoke erb
create app/views/sessions
create app/views/sessions/new.html.erb
invoke test_unit
create test/controllers/sessions_controller_test.rb
invoke helper
create app/helpers/sessions_helper.rb
invoke test_unit
invoke assets
invoke coffee
create app/assets/javascripts/sessions.coffee
invoke scss
create app/assets/stylesheets/sessions.scss
However, there's a discrepancy between what the lesson says routes.rb should look like and what it actually does look like.
Expected result in routes.rb, as implied by the lesson:
Rails.application.routes.draw do
root 'static_pages#home'
get '/help', to: 'static_pages#help'
get '/about', to: 'static_pages#about'
get '/contact', to: 'static_pages#contact'
get '/signup', to: 'users#new'
get '/login', to: 'sessions#new'
post '/login', to: 'sessions#create'
delete '/logout', to: 'sessions#destroy'
resources :users
end
Actual result in routes.rb:
Rails.application.routes.draw do
get 'sessions/new'
root 'static_pages#home'
get '/help', to: 'static_pages#help'
get '/about', to: 'static_pages#about'
get '/contact', to: 'static_pages#contact'
get '/signup', to: 'users#new'
post '/signup', to: 'users#create'
resources :users
end
Am I missing something obvious? Why the difference?
I feel like there's a missing chapter between here and the last one, which only explains the really high level difference between browser-close-ending sessions and cookie-expired sessions.
You ran rails generate controller Sessions new, which means you want to generate a controller with the new action only. By default, new generates the view route (GET) and the actual resource creation route (POST).
To create the controller and routes following the CRUD pattern, you should run rails generate controller Sessions.
Solved.
The reason for the discrepancy is that rails generate controller Sessions new only generates the GET route #vnbrs mentioned.
The lesson calls for 3 routes that need to be supported: a GET request to the login form, a POST request to create the session, and a DELETE request to destroy the session.
Those need to be added to routes.rb manually:
get '/login', to: 'sessions#new'
post '/login', to: 'sessions#create'
delete '/logout', to: 'sessions#destroy'
Beginner's disorientation, I couldn't see the forest for the trees and broke my brain. (P.S. I don't recommend trying to tackle this tutorial discontinuously over a series of months...your brain does NOT pick up where it left off.)
Related
I’ve completed Michael Hartl ch 9 and have no error in testing.
But I could not sign up...
It returns error as below even so previously I be able to sign up..login works fine.
How to fix it and be able to sign up?
Should I merge sign up branch? But doing that I could loose all changes.. Could something be done about sign up form this more advance branch?
Error:
No route matches [POST] "/signup"
routes.rb:
Rails.application.routes.draw do
root 'static_pages#home'
get '/help', to: 'static_pages#help'
get '/about', to: 'static_pages#about'
get '/contact', to: 'static_pages#contact'
get '/signup', to: 'users#new'
get '/login', to: 'sessions#new'
post '/login', to: 'sessions#create'
delete '/logout', to: 'sessions#destroy'
resources :users
end
thanks.
Thanks everybody for answers.
It works now.
One more question related.
When users sign up - they could directly participate in website. It is no good since they need to get confirmation by email and only that they could participate.
How it could be implemented?
I think it is in the solutions Michael Hartl and I even implemented it for Rails 4, but could not remember what need to change..
What files need to be changed to allow users participate in website only after getting confirmation by email.
thanks.
The error is very clear and crisp, you don't have a route for users#create, you need to implement that action and it should work.
Go through this section in the book..
No route matches [POST] "/signup"
You should have post '/signup', to: 'users#create' in your routes.
You need to add
post '/signup', to: 'users#create'
And write create method in
class UsersController < ApplicationController
def create
# Create User with signup parameterss
end
end
I generated a controller and changed the routes but opening the links yields errors on my local server.
Generating controller and routes
rails generate controller StaticPages home about team contact
Change routes.rb
MyApp::Application.routes.draw do
root to: 'static_pages#home'
match '/about', to: 'static_pages#about'
match '/team', to: 'static_pages#team'
match '/contact', to: 'static_pages#contact'
end
The root path work but none of the 'about, 'team', or 'contact' links work. This is the error I get:
"You should not use the match method in your router without specifying an HTTP method. If you want to expose your action to both GET and POST, add via: [:get, :post] option. If you want to expose your action to GET, use get in the router: Instead of: match "controller#action" Do: get "controller#action""
Why can't I use 'match'?
match method has been deprecated.
Use get for GET and post for POST.
get '/about', to: 'static_pages#about'
You can use match, you've gotta add a via: option:
match '/about', to: 'static_pages#about', via: :get
match '/team', to: 'static_pages#team', via: :get
match '/contact', to: 'static_pages#contact', via: :get
You can also pass other HTTP verbs to via: if you need to, like via: [:get, :post]
Source: Rails Routing Guide
First, you must specify the HTTP method by adding via: :get at the end of match 'st' => 'controller#action
And it's better to use get '/home', to: 'static_pages#home'
But, there is a problem, that your code doesn't follow RESTful, that only support 7 actions: index, new, edit, create, update, show and destroy.
These are 2 solutions:
SOL 1: Put them in different controller (homes, abouts..) and all of these controllers have action index.
SOL 2: If it's too much work, we can match them to show action. We use static_pages controller, and each page (home, about) will be a item.
The routes will look likes
/static_pages/home
/static_pages/about
I know it isn't good because of the prefix static_pages.
We can easily get rid of this by adding a custom routes at the end of routes file:
get '/:id', to: 'static_pages#show'
That's it. And if you think it's too much work (I think so too), check out this gem High Voltage. Have fun.
Hope everyone's good this morning. Been learning rails lately, and been working on a little "social" app for practice. I've looked around for a good answer for this, but couldn't find the right thing for me. Here's what I'm trying to figure out now:
By default, the url is hi.com/user/12, and so far I've changed that to /user/name. Everything works fine there. But of course, I want to take it further and do hi.com/name (mostly because I want to learn how it's done properly).
Here's my (abbreviated) routes:
resources :users do
member do
get :following, :followers
end
end
root to: 'pages#home'
match '/help', to: 'pages#help'
match '/about', to: 'pages#about'
match '/contact', to: 'pages#contact'
match '/signup', to: 'users#new'
match '/signin', to: 'sessions#new'
match '/signout', to: 'sessions#destroy', via: :delete
Here's the show view from the controller:
def show
#user = User.find_by_username(params[:id])
#tweets = #user.tweets.paginate(page: params[:page])
end
Now what I've tried is to stick match ':username', to: 'users#show' in my routes, and then change find_by_username(params[:id]) in the controller to find_by_username(params[:username]). This actually works really well, and shows all proper user info on hi.com/name. The following and followers are still (as expected) on user/name/following (and they work fine). However, all static pages break, including /signout (kinda important :p), and while /users still shows an index of all users, users/name throws an error (if /name works, then I wouldn't need /users/name anyway). The error being undefined method `tweets' for nil:NilClass in the users controller.
Again what I'm trying to accomplish is hi.com/username and hi.com/username/following, etc. Also need my static pages to still work. I have everything working with /users/name, but I want to learn how to make it work this way too.
Thanks everyone, I appreciate it!
Basically, it's easy but it has side effects.
Just add at the end of your routes.rb:
get '/:id', to: 'user#show'
But, a user named about, or users or pages won't be able to see their profile, because the path is used earlier in your routes.rb.
Around listing 5.29 of the Hartl rails tutorial (http://ruby.railstutorial.org/) there is a discussion of a routing error that forces you to call the get method on users/new and not just a match method. This is later rectified by calling to the resources method in the next chapter.
My general question is why don't we have to call get() on the PagesController actions in the following listing.
#5.29
SampleApp::Application.routes.draw do
get "users/new"
match '/signup', :to => 'users#new'
match '/contact', :to => 'pages#contact'
match '/about', :to => 'pages#about'
match '/help', :to => 'pages#help'
root :to => 'pages#home'
end
The difference is in the types of HTTP requests the routes will match.
By using get "users/new" the route will only match HTTP GET requests.
match "users/new" would actually match all types of HTTP requests.
You can use get instead of match for your other routes if you only expect GET requests for them (which appears to be the case).
Recently I've tried to update my routes to be more specific in the routing.rb file:
resources :users
match '/signup', :to => 'users#new'
I removed the resources :users and changed the above to
match 'users/new' => 'users#new'
match 'users/show/:id' => 'users#show', :as => :users_show
match 'users/edit' => 'users#edit'
match '/signup', :to => 'users#new'
But when I try to click on the same link that I've had before in my layout page using the signup_path (http://localhost:3000/signup), I get the following error:
undefined method `users_path' but I am not using users_path anywhere
Shouldn't it still work? I haven't changed anything else in the other pages. The controller and actions are still the same.
Thanks!
What it tells is that you don have helper method named "users_path", which is created by resources in routes.rb.
Check by yourself with rake routes, what you get with your example:
users_new /users/new.(:format) {:controller=>"users", :action=>"new"}
users_show /users/show/:id(.:format) {:controller=>"users", :action=>"show"}
users_edit /users/edit(.:format) {:controller=>"users", :action=>"edit"}
(and no, you don't need forward slash at the beginning of path).
You defined only routes for some pages (show/new/edit), but you don't have path for index/create/update/destroy actions. Add resource back to routes.rb and check which routes it generates. Remember that by default your match-ed routes match all requests, not specific HTTP methods (like with resource). If you want to fully reassemble this behavior, take a look at documentation (at http://api.rubyonrails.org search for "match") and :via parameter.
Also why do you want to remove resource, and add all paths by yourself? If you want to limit available routes, then there are :only/:except options, you can also pass block and add routes by yourself, just check documentation (at http://api.rubyonrails.org and search for "resources" - it's shame I no longer can get link to specific documentation page and from Rails documentation).
I think basicxman is on the right track. You need a leading '/' in all your routes
Change:
match 'users/new' => 'users#new'
match 'users/show/:id' => 'users#show', :as => :users_show
match 'users/edit' => 'users#edit'
To:
match '/users/new' => 'users#new'
match '/users/show/:id' => 'users#show', :as => :users_show
match '/users/edit' => 'users#edit'
Also, do you still have the resources :users line at the top of your routes.rb? That'll also be necessary.
You have to add a route to create action as well. Probably your signup form points to users_path with POST method which is basically the create action. So add to your routes the following
match 'users' => 'users#create', :via => :post, :as=>:users
For more customization info please see the Rails routing guide