EDIT: Thanks for the replies, I tried everything listed below but still now luck. If I type localhosts/posts/new it takes me to the form, however the link does not work when I click from the navigation bar. I've updated the code and included my rake routes results.
I am new to ruby and working through a tutorial, however one of my links is not working and not sure what is happening.
My navigation link to create a new post is not taking me to the correct page, when I click the link to the "posts_path" the page does not change.
I can create a new posts by typing in the /posts/new in the address bar, but when I click the "New Post" link on the nav bar the page does not update (although the url displays /posts). Any idea how to fix this?
config/routes.rb
Rails.application.routes.draw do
get 'sessions/new'
root 'static_pages#home'
get '/search', to: 'static_pages#search'
get '/login', to: 'sessions#new'
get '/posts', to: 'posts#new', as: 'new_post'
post '/login', to: 'sessions#create'
delete '/logout', to: 'sessions#destroy'
get '/signup', to: 'users#new'
get 'users/new'
get 'static_pages/home'
get 'posts/new'
get 'sessions/new'
resources :users
resources :account_activations, only: [:edit]
resources :password_resets, only: [:new, :create, :edit, :update]
resources :posts, only: [:new, :create, :destroy]
end
app/views/layouts/_header.html.erb
<header class="navbar navbar-fixed-top navbar-inverse">
<div class="container">
<% link_to "sample app", root_path, id: "logo" %>
<nav>
<ul class="nav navbar-nav navbar-right">
<li><%= link_to "Home", root_path %></li>
<li><%= link_to "Search", search_path %></li>
<% if logged_in? %>
<li><%= link_to "Users", users_path %></li>
<li><%= link_to "Posts", posts_new_path %></li>
<li class="dropdown">
Account <b class="caret"></b>
<ul class="dropdown-menu">
<li><%= link_to "Profile", current_user %></li>
<li><%= link_to "Settings", edit_user_path(current_user) %></li>
<li class="divider"></li>
<li><%= link_to "Logout", logout_path, method: "delete" %></li>
</ul>
</li>
<% else %>
<li><%= link_to "Log in", login_path %></li>
<% end %>
</ul>
</nav>
</div>
posts_controller.rb
class PostsController < ApplicationController
before_action :logged_in_user, only: [:create, :destroy]
before_action :correct_user, only: :destroy
def create
#post = current_user.posts.build(post_params)
if #post.save
flash[:success] = "Post created!"
redirect_to root_url
else
#feed_items = []
render 'static_pages/home'
end
end
def destroy
#post.destroy
flash[:success] = "Post Deleted"
redirect_to request.referrer || root_url
end
def new
#post = current_user.posts.build if logged_in?
end
private
def post_params
params.require(:post).permit(:description, :picture)
end
def correct_user
#post = current_user.posts.find_by(id:params[:id])
redirect_to root_url if #post.nil?
end
end
rake routes:
password_resets_new GET /password_resets/new(.:format) password_resets#new
password_resets_edit GET /password_resets/edit(.:format) password_resets#edit
sessions_new GET /sessions/new(.:format) sessions#new
root GET / static_pages#home
search GET /search(.:format) static_pages#search
login GET /login(.:format) sessions#new
new_post GET /posts(.:format) posts#new
POST /login(.:format) sessions#create
logout DELETE /logout(.:format) sessions#destroy
signup GET /signup(.:format) users#new
users_new GET /users/new(.:format) users#new
static_pages_home GET /static_pages/home(.:format) static_pages#home
static_pages_about GET /static_pages/about(.:format) static_pages#about
static_pages_search GET /static_pages/search(.:format) static_pages#search
posts_new GET /posts/new(.:format) posts#new
GET /password_resets/new(.:format) password_resets#new
GET /password_resets/edit(.:format) password_resets#edit
GET /sessions/new(.:format) sessions#new
help GET /help(.:format) static_pages#help
about GET /about(.:format) static_pages#about
contact GET /contact(.:format) static_pages#contact
users GET /users(.:format) users#index
POST /users(.:format) users#create
new_user GET /users/new(.:format) users#new
edit_user GET /users/:id/edit(.:format) users#edit
user GET /users/:id(.:format) users#show
PATCH /users/:id(.:format) users#update
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy
edit_account_activation GET /account_activations/:id/edit(.:format) account_activations#edit
password_resets POST /password_resets(.:format) password_resets#create
new_password_reset GET /password_resets/new(.:format) password_resets#new
edit_password_reset GET /password_resets/:id/edit(.:format) password_resets#edit
password_reset PATCH /password_resets/:id(.:format) password_resets#update
PUT /password_resets/:id(.:format) password_resets#update
posts POST /posts(.:format) posts#create
GET /posts/new(.:format) posts#new
post DELETE /posts/:id(.:format) posts#destroy
Remove get 'sessions/new' & get '/posts', to: 'posts#new', as: 'new_post'
Restart server
and try link_to "Posts", new_post_path, method: :post
Should work
Rails scans the routes from top to bottom i.e. routes.rb. Whenever it finds the matching routes corresponding controller#action gets executed and remaining all will be skipped(later routes after match found).
In this case routes are conflicting, So do bellow changeseverything will work fine.
config/routes.rb
Rails.application.routes.draw do
get 'sessions/new'
root 'static_pages#home'
get '/search', to: 'static_pages#search'
get '/login', to: 'sessions#new'
#get '/posts', to: 'posts#new', as: 'new_post' #not required as resources :posts will do the job in last line
post '/login', to: 'sessions#create'
delete '/logout', to: 'sessions#destroy'
get '/signup', to: 'users#new'
get 'users/new'
get 'static_pages/home'
get 'posts/new', to: 'posts#new', as: 'posts_new_path' #Made changes here according to helper used in view 'posts_new_path'
get 'sessions/new'
resources :users
resources :account_activations, only: [:edit]
resources :password_resets, only: [:new, :create, :edit, :update]
resources :posts, only: [:new, :create, :destroy]
end
I would suggest that you remove get 'posts/new' & get '/posts', to: 'posts#new', as: 'new_post' as these are duplicate routes.
You already have the correct route created in the route resources :posts, only: [:new, :create, :destroy]. By including new, Rails is doing this portion for you. See here: http://guides.rubyonrails.org/v4.2/routing.html#crud-verbs-and-actions
Then you should be able to use the link that everyone else has posted.
<%= link_to "New Post", new_post_path %> based on the guide here: http://guides.rubyonrails.org/v4.2/routing.html#path-and-url-helpers
The problem is there is conflict occurring in your routes.
You have defined routes twice for new
get /posts', to: 'posts#new', as: 'new_post'
and
resources :posts, only: [:new, :create, :destroy]
now there are 2 paths for posts#new. I suggest u remove first one. Change your html to
<li><%= link_to 'New Post', posts_path %></li>
and it should work fine.
i went through your code...and this is what i recommend to add/edit changes..and sure it should work :).
for routes.rb,its looks good BUT
there is a duplicate url entry.So either remove get 'posts/new' or resources :posts, only: [:new, :create, :destroy] as they both will create a duplicate entry for posts#new.You can verify this by outputting your routes in seperate txt file by rake routes >> path.txt
in app/views/layouts/_header.html.erb,its good too and dont need any change.
in posts_controller.rb,there is a condition added for only logged in user,Kindly check if you are logged in else remove that condition and check again.
Finally,debug using F12 in chrome/firefox and see what log do you get in web browser console before and after click to check is there some javascript error or not.
code for link to new post
<%= link_to "New Post", new_post_path %>
Try new_post_path
Also, rake routes in terminal will list all the routes and associated helpers for you to double check against.
Finally, adding 'as' to: get '/posts', to: 'posts#new', as: 'new_post' in your routes file will allow you to define your own path name (in this case new_post_path, but you can set it as anything you'd like).
<%= link_to "Posts", posts_path %> corresponds to index action not to the new action.
Remove the only condition for your posts controller in routes.rb which will create all the basic routes needed rather than specific routes.
resources :posts
So, new_post_path returns /posts/new
Finally add a link link <%= link_to 'New Post', new_post_path %>
This may be helps to redirect,
in routes.rb,
resources :posts
It gives the paths to all default controller methods like index, new, create, edit, update, show, destroy. Then check with rake routes. Get the corresponding path like,
new_post GET /posts/new(.:format) posts#new
if it correct means give the url like,
<%= link_to 'New Post', new_post_path %>
It redirects to method new in controller and render corresponding html.erb page. This url formed by the routes.rb.
OR
push the redirection url manually.In html page,
<%= button_tag "New", :onclick => "getNew()" %>
function getNew(){
var form = document.forms[0];
// get the id and value with any variable //
form.method = "post";
// pass the value to parameters //
form.var name = column name; (description r picture)
form.action = "posts/new";
form.submit();
}
in routes.rb,
match '/posts/new' => 'posts#new', via: [:get, :post]
It makes the manual redirection url. It calls the new in controller method.
Let me know your feedback! Thanks
As per your rake routes output the following should work
<%= link_to "New Post", posts_new_path %>
Please let us know whether there any error comming in the console log on clicking the new post link.
The bottom line is that you're trying to do things in a non-Rails manner. Rails favors convention over configuration, meaning if you don't do things the Rails way, you're going to have a bad time.
This means you should clean up your routes until you have no unnecessary duplication. Right now you have 3 different routes that go to posts#new each with a different path prefix.
I'd remove:
get '/posts', to: 'posts#new', as: 'new_post'
get 'posts/new'
since resources :posts, only: [:new, :create, :destroy] already creates that route. Then your new_post_path helper should create the correct URL and your routes will have no ambiguity.
Related
I'm using the gem Monban for user authentication am trying to implement modal usage for users to login/signup/edit etc. I am able to sign up a user through bootstrap modal use no problem at all using form_for User.new etc, however I can't do the same to log a user in. I've tried form_for Session.new but it errors and says uninitialized constant on homepage load, before even seeing the modal. I've tried form_for new_session_path, and i see the modal this time but get the error message No route matches [POST] "/". I've also tried using form_tag and other combinations but still no luck. If anyone can point me in the right direction that would be great! Please see my code below:
view/modals/_signin.html.erb
<div id="signin-modal" class="modal fade" tabindex="-1" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title">Welcome Back!</h4>
</div>
<div class="modal-body">
<%= form_for Session.new do |f| %>
<div>
<span class="model-label"><%= f.label :username %></span>
<p><%= f.text_field :username, placeholder: "Username..", required: true, class: "modal-input" %><p>
</div>
<div>
<span class="model-label"><%= f.label :password %></span>
<p><%= f.password_field :password, require: true, placeholder: "Password..", class: "modal-input" %></p>
</div>
</div>
<div class="modal-footer">
<%= f.submit "Sign In", class: "btn btn-info" %>
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<% end %>
</div>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
</div><!-- /.modal -->
sessions_controller.rb
class SessionsController < ApplicationController
skip_before_action :require_login, only: [:new, :create], raise: false
def new
end
def create
user = authenticate_session(session_params)
if sign_in(user)
redirect_to root_path
else
render :new
end
end
def destroy
sign_out
redirect_to root_path
end
private
def session_params
params.require(:session).permit(:username, :password)
end
end
config/routes.rb
Rails.application.routes.draw do
resources :chats, only: [:show, :update, :index] do
resources :comments, only: [:new, :create]
end
resources :comments, only: [:edit, :update, :destroy]
resources :posts, only: [:edit, :update, :destroy]
resources :locations, only: [:edit, :update]
resources :profiles, only: [:show, :edit, :update] do
resources :chats, only: [:new, :create]
resources :locations, only: [:new, :create], module: :profiles
resources :posts, only: [:new, :create]
end
resource :session, only: [:new, :create, :destroy]
resources :users, only: [:new, :create, :index, :update] do
post 'follow', to: 'following_relationships#create'
delete 'follow', to: 'following_relationships#destroy'
post 'tutor', to: 'tutoring_relationships#create'
delete 'tutor', to: 'tutoring_relationships#destroy'
end
root 'home#index'
end
which give me the following routes
Prefix Verb URI Pattern Controller#Action
chat_comments POST /chats/:chat_id/comments(.:format) comments#create
new_chat_comment GET /chats/:chat_id/comments/new(.:format) comments#new
chats GET /chats(.:format) chats#index
chat GET /chats/:id(.:format) chats#show
PATCH /chats/:id(.:format) chats#update
PUT /chats/:id(.:format) chats#update
edit_comment GET /comments/:id/edit(.:format) comments#edit
comment PATCH /comments/:id(.:format) comments#update
PUT /comments/:id(.:format) comments#update
DELETE /comments/:id(.:format) comments#destroy
edit_post GET /posts/:id/edit(.:format) posts#edit
post PATCH /posts/:id(.:format) posts#update
PUT /posts/:id(.:format) posts#update
DELETE /posts/:id(.:format) posts#destroy
edit_location GET /locations/:id/edit(.:format) locations#edit
location PATCH /locations/:id(.:format) locations#update
PUT /locations/:id(.:format) locations#update
profile_chats POST /profiles/:profile_id/chats(.:format) chats#create
new_profile_chat GET /profiles/:profile_id/chats/new(.:format) chats#new
profile_locations POST /profiles/:profile_id/locations(.:format) profiles/locations#create
new_profile_location GET /profiles/:profile_id/locations/new(.:format) profiles/locations#new
profile_posts POST /profiles/:profile_id/posts(.:format) posts#create
new_profile_post GET /profiles/:profile_id/posts/new(.:format) posts#new
edit_profile GET /profiles/:id/edit(.:format) profiles#edit
profile GET /profiles/:id(.:format) profiles#show
PATCH /profiles/:id(.:format) profiles#update
PUT /profiles/:id(.:format) profiles#update
session POST /session(.:format) sessions#create
new_session GET /session/new(.:format) sessions#new
DELETE /session(.:format) sessions#destroy
user_follow POST /users/:user_id/follow(.:format) following_relationships#create
DELETE /users/:user_id/follow(.:format) following_relationships#destroy
user_tutor POST /users/:user_id/tutor(.:format) tutoring_relationships#create
DELETE /users/:user_id/tutor(.:format) tutoring_relationships#destroy
users GET /users(.:format) users#index
POST /users(.:format) users#create
new_user GET /users/new(.:format) users#new
user PATCH /users/:id(.:format) users#update
PUT /users/:id(.:format) users#update
root GET / home#index
Thanks, and please let me know if you have any ideas!
I see following in Rails documentation for singular resources (resource command)
A long-standing bug prevents form_for from working automatically with
singular resources. As a workaround, specify the URL for the form
directly, like so:
resource :geocoder
form_for #geocoder, url: geocoder_path do |f|
So should be something like this in your case:
<%= form_for Session.new, url: session_path do |f| %>
By the way, new_session_path is not what you want anyway, as new is for the form rendering and not saving (creating) the object.
After implementing my latest addition to my rails app (authentication using bcrypt), I realized my rails app can no longer route to any of the links in the static_pages controller. Is there a way around this? Is it a problem with my route implementation?
Before: localhost:3000/home
After Login: localhost:3000/users/2
Error that I get when I click on home or any other link after login:
GET localhost:3000/users/home 404 (Not Found)
My routes:
Rails.application.routes.draw do
resources :user
root 'static_pages#home'
match '/alex' , to: 'static_pages#alex', via: 'get'
match '/help', to: 'static_pages#help', via: 'get'
match '/about', to: 'static_pages#about', via: 'get'
match '/home', to:'static_pages#home', via: 'get'
match '/contact', to:'static_pages#contact', via:'get'
match 'users/show' , to: 'users#show', via: 'get'
get 'login' => 'sessions#new'
post 'login' => 'sessions#create'
delete 'logout' => 'sessions#destroy'
get '/signup' => 'users#new'
resources :sessions, only: [:new, :create, :destroy]
end
My Header File located in Layouts:
<div id = "navbar">
<ul id = "menu-nav">
<li class = "nav-item"> </i> Home </li>
<li class = "nav-item"> <i class="fa fa-music"></i> Music </li>
<li class = "nav-item"> </i> Artist Discovery </li>
<li class = "nav-item"> <i class="fa fa-paper-plane"> </i> News </li>
<li class = "nav-item"> <i class="fa fa-heart"></i> Alex! </li>
<% if #users == nil %>
<li class = "nav-item"> <i class="fa fa-heart"></i> Sign Up </li>
<li class = "nav-item"> <i class="fa fa-heart"></i> Login </li>
<% end %>
</ul>
</div>
Rake Routes:
Prefix Verb URI Pattern Controller#Action
root GET / static_pages#home
alex GET /alex(.:format) static_pages#alex
help GET /help(.:format) static_pages#help
about GET /about(.:format) static_pages#about
home GET /home(.:format) static_pages#home
contact GET /contact(.:format) static_pages#contact
users_show GET /users/show(.:format) users#show
login GET /login(.:format) sessions#new
POST /login(.:format) sessions#create
logout DELETE /logout(.:format) sessions#destroy
signup GET /signup(.:format) users#new
users GET /users(.:format) users#index
POST /users(.:format) users#create
new_user GET /users/new(.:format) users#new
edit_user GET /users/:id/edit(.:format) users#edit
user GET /users/:id(.:format) users#show
PATCH /users/:id(.:format) users#update
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy
sessions POST /sessions(.:format) sessions#create
new_session GET /sessions/new(.:format) sessions#new
session DELETE /sessions/:id(.:format) sessions#destroy
Try replacing your 'home' link with this:
<%= link_to "Home", home_url %>
Use slashes for before url on a tag
<a href='/home' > Home </a>
or use Rails option, all these should work...
<%= link_to 'Home', root_path %>
<%= link_to 'Home', home_path %>
<%= link_to 'Home', home_url %>
One more thing.
If you are using newer version of Rails < 4, then suggesting to use get instead of match.
get "home" => 'static_page#home'
I receive an error saying that it has an
undefined local variable or method 'your_questions_path'
My routes.rb file:
Rails.application.routes.draw do
get "/" => "main_app#index"
get "/location" => "location#location"
post "/location/index" => "location#index"
get "/location/index" => "location#index"
get "/location/directions" => "location#directions"
root to: 'questions#index'
resources :questions do
collection do
get :your_questions
end
end
get '/logout', to: 'sessions#destroy', via: :delete
resources :users, only: [:new, :create]
resources :sessions, only: [:new, :create]
resources :questions, except: [:new] do
resources :answers, only: [:create]
end
get '/register', to: 'users#new'
get '/login', to: 'sessions#new'
get '/logout', to: 'sessions#destroy', via: :delete
# get '/questions/your_questions', to: 'questions#your_questions' original
get '/questions/:id', to: 'questions#show'
get 'search', to: 'controller#action', as: :search
My view file with the questions path a.k.a app/views/layouts/application.html.erb
<div id="nav">
<ul>
<li>
<%= link_to 'Home', root_path %>
</li>
<% if logged_in? %>
<li>
<%= link_to "Your Q's", your_questions_path %>
</li>
<li>
<%= link_to "Logout (#{current_user.username})", logout_path, method: 'get' %>
</li>
<% else %>
<li>
<%= link_to 'Register', register_path %>
</li>
<li>
<%= link_to 'Login', login_path %>
</li>
I want the questions path to go to http://localhost:3000/questions/1
Thank you in advance!
The routes you've defined create the following routes:
your_questions_questions GET /questions/your_questions(.:format) questions#your_questions
questions GET /questions(.:format) questions#index
POST /questions(.:format) questions#create
new_question GET /questions/new(.:format) questions#new
edit_question GET /questions/:id/edit(.:format) questions#edit
question GET /questions/:id(.:format) questions#show
PATCH /questions/:id(.:format) questions#update
PUT /questions/:id(.:format) questions#update
DELETE /questions/:id(.:format) questions#destroy
The top route is the your_questions route, which means you want use:
your_questions_questions_path
this is happening because you have nested your path inside of a resources block, ie. you have this:
resources :questions do
collection do
get :your_questions
end
end
if you were to get to this path, (run rake routes) you would need to use questions_your_questions_path, however since you want to route to something like http://localhost:3000/quesitons/1 this is a simple 'show' path that is provided to you by the resources command, instead change your routes file to simply say:
resources :questions
and then use question_path(#question) where #question is an instance variable set in the controller.
However... also looking from your code, you seem to be wanting the questions_path instead since that would provide the index action (a listing of questions). You can then in your controller scope the collection of questions to the ones the current user sees...
ie.
QuestionsController < ApplicationController
def index
#questions = current_user.questions
end
(assuming your User has_many :questions)
I'm sorry but I am fairly new to Rails and I can't seem to understand what the problem is. I am building an online forum and want my users to not only be able to edit their own post but also any comments that they may create. I keep getting a: undefined method `comment_path' for #<#:0x007fe5b6b0cbd0>. Any ideas?
routes:
PostitTemplate::Application.routes.draw do
root to: 'posts#index'
get '/register', to: 'users#new'
get '/login', to: 'sessions#new'
post '/login', to: 'sessions#create'
get '/logout', to: 'sessions#destroy'
resources :users, only: [:create, :edit, :update]
resources :posts, except: [:destroy] do
member do
post 'vote'
end
resources :comments, only: [:create, :edit, :update] do
member do
post 'vote'
end
end
end
resources :categories, only: [:new, :create]
end
my comments edit.html.erb:
<div class="page-header">
<h2>Update Comment<small> - looks like you need some updating!</small></h2>
</div>
<h3><%= #post.description %></h3>
<%= render 'shared_partials/errors', errors_obj: #comment %>
<div class="well">
<%= form_for #comment do |f| %>
<%= f.text_area :body, :class=> "input", :placeholder=> "Comment goes here", :rows => "6" %>
</br>
<div class="button">
<%= f.submit "Create a comment", class: 'btn btn-primary' %>
</div>
<% end %>
</div>
comments_controller:
class CommentsController < ApplicationController
before_action :require_user
def create
#post = Post.find(params[:post_id])
#comment = Comment.new(params.require(:comment).permit(:body))
#comment.post = #post
#comment.creator = current_user
if #comment.save
flash[:notice] = "Your comment was created!"
redirect_to post_path(#post)
else
render 'posts/show'
end
end
def edit
#comment = Comment.find(params[:id])
#post = Post.find(params[:post_id])
end
def update
#comment = Comment.find(params[:id])
if #comment.update(comment_params)
flash[:notice] = "You updated your comment!"
redirect_to post_comments_path
else
render :edit
end
end
end
rake routes:
Prefix Verb URI Pattern Controller#Action
root GET / posts#index
register GET /register(.:format) users#new
login GET /login(.:format) sessions#new
POST /login(.:format) sessions#create
logout GET /logout(.:format) sessions#destroy
users POST /users(.:format) users#create
edit_user GET /users/:id/edit(.:format) users#edit
user PATCH /users/:id(.:format) users#update
PUT /users/:id(.:format) users#update
vote_post POST /posts/:id/vote(.:format) posts#vote
vote_post_comment POST /posts/:post_id/comments/:id/vote(.:format) comments#vote
post_comments POST /posts/:post_id/comments(.:format) comments#create
edit_post_comment GET /posts/:post_id/comments/:id/edit(.:format) comments#edit
post_comment PATCH /posts/:post_id/comments/:id(.:format) comments#update
PUT /posts/:post_id/comments/:id(.:format) comments#update
posts GET /posts(.:format) posts#index
POST /posts(.:format) posts#create
new_post GET /posts/new(.:format) posts#new
edit_post GET /posts/:id/edit(.:format) posts#edit
post GET /posts/:id(.:format) posts#show
PATCH /posts/:id(.:format) posts#update
PUT /posts/:id(.:format) posts#update
categories POST /categories(.:format) categories#create
new_category GET /categories/new(.:format) categories#new
Because your comments are nested resources, you'll need to pass the #post into your call to form_for().
form_for [#post, #comment] do |f|
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
I am studying Ruby on rails by Michael Hartl. I am stuck at the section 8.1.4 which is implementing a sign-in page using Rails 3.2.3 with Ruby 1.9.3-p125.
I have created a session controller, and I want my session controller's create action maps to this route /sessions , but always a routing error. Any clues? The following are my relevant files:
routes.rb
SampleApp::Application.routes.draw do
resources :users
resources :sessions, only: [:new, :create, :destroy]
root to: 'static_pages#home'
match '/signup', to: 'users#new'
match '/signin', to: 'sessions#new'
match '/signout', to: 'sessions#destroy', via: :delete
match '/help', to: 'static_pages#help'
match '/about', to: 'static_pages#about'
match '/contact', to: 'static_pages#contact'
end
and my rake routes :
users GET /users(.:format) users#index
POST /users(.:format) users#create
new_user GET /users/new(.:format) users#new
edit_user GET /users/:id/edit(.:format) users#edit
user GET /users/:id(.:format) users#show
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy
sessions POST /sessions(.:format) sessions#create
new_session GET /sessions/new(.:format) sessions#new
session DELETE /sessions/:id(.:format) sessions#destroy
root / static_pages#home
signup /signup(.:format) users#new
signin /signin(.:format) sessions#new
signout DELETE /signout(.:format) sessions#destroy
help /help(.:format) static_pages#help
about /about(.:format) static_pages#about
contact /contact(.:format) static_pages#contact
my app/views/sessions/new.html.erb :
<%= provide(:title, 'Sign in') %>
<h1>Sign in</h1>
<div class="row">
<div class="span6 offset3">
<%= form_for(:session, url: sessions_path) do |f| %>
<%= f.label :email %>
<%= f.text_field :email %>
<%= f.label :password %>
<%= f.password_field :password %>
<%= f.submit "Sign in", class: "btn btn-large btn-primary" %>
<% end %>
<p>New user? <%= link_to "Sign up now!", signup_path %></p>
</div>
my sessions controller:
class SessionsController < ApplicationController
def new
end
def create
render 'new'
end
def destroy
end
end
In figure 8.5 the picture shows the url for sign in is http://localhost:3000/sessions. When I navigate to that page,
I always get a
Routing Error
No route matches [GET] "/sessions"
solved
The figure 8.5 is showing the page after you hit the create button, and the url changes from /sessions/new or /signin to /sessions, So it's nothing wrong with my above files.
I think it's a typo. You have to naviagte to http://localhost:3000/sessions/new as indicated in your routes.