I am following along this tutorial's authentication process:
http://larsgebhardt.de/user-authentication-with-ruby-on-rails-rspec-and-capybara/
Since the tutorial is based off Rails 3, according to the last commenter in the article above, there are only a few modifications needed to make the tutorial compatible with Rails 4. I made those changes but still continue to chase my tail with issues. I have posted questions 2 other times on Stack Overflow and although I'm getting answers that 'fix' the current issue at hand, I'm afraid I might be straying away from the tutorial's approach of authentication from scratch - further breaking the tests which is causing a domino effect of issues.
For a bit of history, here is the last two questions in chronological order..
Rspec email_spec issue
Rspec & Capybara: uninitialized constant SessionController
So here's the failure I have now..
5) User Management User log out
Failure/Error: activate(#writer)
ActionView::Template::Error:
undefined method `current_user' for #<SessionsController:0x007fd2029c8d68>
users_spec.rb..
feature 'User Management' do
background do
#writer = create(:user, :writer)
end
scenario 'User log out' do
activate(#writer)
login(#writer)
logout(#writer)
expect(page).to have_content "Successfully logged out."
end
The method exists in my profiles_controller.rb - which is similar to the tutorial's offices_controller.rb
class ProfilesController < ApplicationController
def show
auth_required
access_only_with_roles("writer", "admin")
end
def current_user
#current_user ||= User.find(session[:user_id]) if session[:user_id]
end
end
This is the _header.html.erb file with the Log Out link code.
<%= link_to "Sign Up", new_user_path %>
<%= link_to "Log In", new_session_path %>
<%= link_to "Log Out", session_path(current_user), method: :delete %>
My routes.rb..
Rails.application.routes.draw do
get 'profiles/show'
get 'sessions/new'
get 'users/new'
# The priority is based upon order of creation: first created -> highest priority.
# See how all your routes lay out with "rake routes".
# You can have the root of your site routed with "root"
root 'sessions#new'
resources :posts do
resources :comments
end
resources :sessions
resources :users
resources :profile
get "activate/:code" => "users#activate", :as => "activate"
..and rake routes
Prefix Verb URI Pattern Controller#Action
profiles_show GET /profiles/show(.:format) profiles#show
sessions_new GET /sessions/new(.:format) sessions#new
users_new GET /users/new(.:format) users#new
root GET / sessions#new
post_comments GET /posts/:post_id/comments(.:format) comments#index
POST /posts/:post_id/comments(.:format) comments#create
new_post_comment GET /posts/:post_id/comments/new(.:format) comments#new
edit_post_comment GET /posts/:post_id/comments/:id/edit(.:format) comments#edit
post_comment GET /posts/:post_id/comments/:id(.:format) comments#show
PATCH /posts/:post_id/comments/:id(.:format) comments#update
PUT /posts/:post_id/comments/:id(.:format) comments#update
DELETE /posts/:post_id/comments/:id(.:format) comments#destroy
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
DELETE /posts/:id(.:format) posts#destroy
sessions GET /sessions(.:format) sessions#index
POST /sessions(.:format) sessions#create
new_session GET /sessions/new(.:format) sessions#new
edit_session GET /sessions/:id/edit(.:format) sessions#edit
session GET /sessions/:id(.:format) sessions#show
PATCH /sessions/:id(.:format) sessions#update
PUT /sessions/:id(.:format) sessions#update
DELETE /sessions/:id(.:format) sessions#destroy
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
profile_index GET /profile(.:format) profile#index
POST /profile(.:format) profile#create
new_profile GET /profile/new(.:format) profile#new
edit_profile GET /profile/:id/edit(.:format) profile#edit
profile GET /profile/:id(.:format) profile#show
PATCH /profile/:id(.:format) profile#update
PUT /profile/:id(.:format) profile#update
DELETE /profile/:id(.:format) profile#destroy
activate GET /activate/:code(.:format) users#activate
I would love to figure this out once and for all. It appeared to be a great tutorial. If I figure out the changes then I plan to post them in the comment section of the article for anyone else who plans to use it.
You are getting undefined methodcurrent_user' because you defined current_user in the profilesController,` so it won't be accessible to the view in which you are using it. The solution is to move this method to the applicationController like this :
class ApplicationController < ActionController::Base
protect_from_forgery
helper_method :current_user
private
def current_user
#current_user ||= User.find(session[:user_id]) if session[:user_id]
end
end
So now you can use current_user in any view folder. I just checked the tutorial and it tells you to add this method to applicationController, so you probably added it to profilesController by mistake.
Related
I'm a rails beginner struggling with routes a bit. I'm working on a Q&A site (or question and response) and any time I try to post a response, it gives the following error:
No route matches [GET] "/questions/6/responses"
Any help would be greatly appreciated,
Jon
Here are the relevant bits:
Routes output (given w the error)
(apologies for nasty paste):
Helper HTTP Verb Path Controller#Action
Path / Url
welcome_index_path GET /welcome/index(.:format) welcome#index
users_path GET /users(.:format) users#index
POST /users(.:format) users#create
new_user_path GET /users/new(.:format) users#new
edit_user_path GET /users/:id/edit(.:format) users#edit
user_path GET /users/:id(.:format) users#show
PATCH /users/:id(.:format) users#update
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy
categories_path GET /categories(.:format) categories#index
POST /categories(.:format) categories#create
new_category_path GET /categories/new(.:format) categories#new
edit_category_path GET /categories/:id/edit(.:format) categories#edit
category_path GET /categories/:id(.:format) categories#show
PATCH /categories/:id(.:format) categories#update
PUT /categories/:id(.:format) categories#update
DELETE /categories/:id(.:format) categories#destroy
question_responses_path POST /questions/:question_id/responses(.:format) responses#create
questions_path GET /questions(.:format) questions#index
POST /questions(.:format) questions#create
new_question_path GET /questions/new(.:format) questions#new
edit_question_path GET /questions/:id/edit(.:format) questions#edit
question_path GET /questions/:id(.:format) questions#show
PATCH /questions/:id(.:format) questions#update
PUT /questions/:id(.:format) questions#update
DELETE /questions/:id(.:format) questions#destroy
root_path GET / welcome#index
Routes:
Rails.application.routes.draw do
get 'welcome/index'
resources :users
resources :categories
resources :questions do
resources :responses, :only => [:create]
end
root 'welcome#index'
Questions controller
https://github.com/joncarpe/snack/blob/master/app/controllers/questions_controller.rb
Responses controller
https://github.com/joncarpe/snack/blob/master/app/controllers/responses_controller.rb
You have only route to create action in ResponsesController. If you also want route to index, you should have:
resources :responses, only: [:create, :index]
If you want routes to all default resources actions, you should abandon only option, like this:
resources :responses
I have write down in routes
match '/signup', to: 'users#new', via: [:get, :post]
when i submit the form show me error
My form code is like this
<%= form_for #user, url: {action: "new"} do |f| %>
And form submit on this url not on singup
http://localhost:3000/users/new
and show me error
No route matches [POST] "/users/new"
Rails.root: /home/jaskaran/rails_project
when i check rake output, it's show me
jaskaran#jaskaran-Vostro-1550:~/rails_project$ rake routes
Prefix Verb URI Pattern Controller#Action
root GET / static_pages#home
help GET /help(.:format) static_pages#help
about GET /about(.:format) static_pages#about
contact GET /contact(.:format) static_pages#contact
microposts GET /microposts(.:format) microposts#index
POST /microposts(.:format) microposts#create
new_micropost GET /microposts/new(.:format) microposts#new
edit_micropost GET /microposts/:id/edit(.:format) microposts#edit
micropost GET /microposts/:id(.:format) microposts#show
PATCH /microposts/:id(.:format) microposts#update
PUT /microposts/:id(.:format) microposts#update
DELETE /microposts/:id(.:format) microposts#destroy
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
signup POST|GET|POST /signup(.:format) users#new
posts_new GET /posts/new(.:format) posts#new
posts_create POST /posts/create(.:format) posts#create
I am learning ruby and rails 4 +
Even though your route maps to the new action of the users controller, your form needs to point to the URI pattern, in your case /signup.
When in doubt, you can find the URI pattern and other information in your routes. Run rake routes in your console and you'll see this:
signup GET|POST /signup(.:format) users#new
Rails generates helper methods to use in your views for each of the routes, based on the route prefix. In your case you can use the helper method signup_path. To fix the problem, change your view code to this:
<%= form_for #user, url: signup_path do |f| %>
Check the Rails documentation for more information.
I get this error:
Failures:
1) UsersController DELETE 'destroy' should sign a user out
Failure/Error: delete :destroy
ActionController::UrlGenerationError:
No route matches {:controller=>"users", :action=>"destroy"}
My test is:
it "should sign a user out" do
test_sign_in(Factory(:user))
delete :destroy
expect(controller).to_not be_signed_in
expect(response).to redirect_to(root_path)
end
The test_sign_in function is in the spec helper:
def test_sign_in(user)
controller.sign_in(user)
end
My rake routes:
Prefix Verb URI Pattern Controller#Action
sessions_new GET /sessions/new(.:format) sessions#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
root GET / pages#home
contact GET /contact(.:format) pages#contact
about GET /about(.:format) pages#about
help GET /help(.:format) pages#help
signup GET /signup(.:format) users#new
signin GET /signin(.:format) sessions#new
signout GET /signout(.:format) sessions#destroy
pages_home GET /pages/home(.:format) pages#home
Anyone knows how can I solve this error?
Your route is defined as
DELETE /users/:id(.:format) users#destroy
which means that the route is expecting something like
DELETE /users/4
Looking at your test, you are just requesting DELETE /users, this was derived from this error message:
ActionController::UrlGenerationError: No route matches {:controller=>"users", :action=>"destroy"})
So, you need to modify your test to handle the :id part of the route. This is un-tested, but you're roughly looking for:
user = Factory(:user)
test_sign_in(user)
delete :destroy, id: user.id
ooooh god!! I found the error. I accidentally was writing the code in the users_controller_spec and I should do this in the sessions_controller_spec.
Thanks Andreas for trying to help!
I have a search page and a search button on it, and when I click on the search button, it gave me an error below:
No route matches [POST] "/searching"
And I want that when I click on the search button, It goes into the search page, and show result of search. below is search page controller:
def search
#students=Students.all
#blah = params[:tf_Zip]
puts #blah
if ( !params[:tf_Zip].blank? or params[:tf_Zip] !="" )
#user_zip = User.where(user_Zip: params[:tf_zip])
end
render 'search'
end
You can see above that I am render here to the search page, and below is routes.rb page:
resources :search, only: [:search, :create]
match '/searching', to: 'search#search', via: 'get'
And below is the rake routes:
Helper HTTP Verb Path Controller#Action
Path / Url
search_new_path GET /search/new(.:format) search#new
search_create_path GET /search/create(.:format) search#create
settings_new_path GET /settings/new(.:format) settings#new
educations_create_path GET /educations/create(.:format) educations#create
educations_destroy_path GET /educations/destroy(.:format) educations#destroy
professions_create_path GET /professions/create(.:format) professions#create
professions_destroy_path GET /professions/destroy(.:format) professions#destroy
communications_create_path GET /communications/create(.:format) communications#create
communications_destroy_path GET /communications/destroy(.:format) communications#destroy
availabilities_create_path GET /availabilities/create(.:format) availabilities#create
availabilities_destroy_path GET /availabilities/destroy(.:format) availabilities#destroy
users_path GET /users(.:format) users#index
POST /users(.:format) users#create
new_user_path GET /users/new(.:format) users#new
edit_user_path GET /users/:id/edit(.:format) users#edit
user_path 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_path POST /sessions(.:format) sessions#create
new_session_path GET /sessions/new(.:format) sessions#new
session_path DELETE /sessions/:id(.:format) sessions#destroy
availabilities_path POST /availabilities(.:format) availabilities#create
availability_path DELETE /availabilities/:id(.:format) availabilities#destroy
communications_path POST /communications(.:format) communications#create
communication_path DELETE /communications/:id(.:format) communications#destroy
professions_path POST /professions(.:format) professions#create
profession_path DELETE /professions/:id(.:format) professions#destroy
educations_path POST /educations(.:format) educations#create
education_path DELETE /educations/:id(.:format) educations#destroy
settings_path POST /settings(.:format) settings#create
new_setting_path GET /settings/new(.:format) settings#new
search_index_path POST /search(.:format) search#create
root_path GET / static_pages#home
signup_path GET /signup(.:format) users#new
signin_path GET /signin(.:format) sessions#new
signout_path DELETE /signout(.:format) sessions#destroy
default_path GET /default(.:format) static_pages#default
GET /availabilities(.:format) availabilities#new
GET /communications(.:format) communications#new
GET /professions(.:format) professions#new
GET /educations(.:format) educations#new
GET /settings(.:format) settings#new
searching_path GET /searching(.:format) search#search
Kindly help me, waiting for your reply. Thanks
try this one, as you're sending your form with POST
match '/searching', to: 'search#search', via: 'post'
Ive been following some rails guides and im trying to now implement something on my own for a project im doing and have hit a snag at the first hurdle
I get this error when trying to load the page
ActionController::UrlGenerationError in StepOne#login
Showing /Users/rogan/Sites/authImp/app/views/step_one/login.html.erb where line #3 raised:
No route matches {:action=>"show", :controller=>"step_one"} missing required keys: [:id]
Extracted source (around line #3):
<%= form_for url: step_one_path do %>
form stuff...
then my step_one_controller.rb
class StepOneController < ApplicationController
def new
end
def create
user = User.authenticate(params[:email], params[:password])
if user
pincode = generatePin
puts "one use pin.#{pincode}"
redirect_to "step_two"
else
flash.now.alert = "Invalid email or password"
render "new"
end
end
end
this was adapter from a login system i saw in a guide that used SessionsController.rb and form_for url: sessions_path
but my simple changes seem to have broke it, i've looked at my routes as well and they all seem to be in order
edit: heres is my routes
edit edit: changed everything to step_one and to removed the 's' as suggested, I now get
NoMethodError in StepOne#login
undefined method `model_name' for Hash:Class
<%= form_for url: step_one_path do %>
so from one problem to another!
AuthImp::Application.routes.draw do
resources :users
resources :sessions
resource :step_one
get "users/:id" => "users#show"
get "sign_up" => "users#new"
get "log_in" => "step_one#login"
get "step_two" => "sessions#new"
get "log_out" => "sessions#destroy", :as => "log_out"
then my rake routes is as follows
root to: "welcome#index"
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 GET /sessions(.:format) sessions#index
POST /sessions(.:format) sessions#create
new_session GET /sessions/new(.:format) sessions#new
edit_session GET /sessions/:id/edit(.:format) sessions#edit
session GET /sessions/:id(.:format) sessions#show
PATCH /sessions/:id(.:format) sessions#update
PUT /sessions/:id(.:format) sessions#update
DELETE /sessions/:id(.:format) sessions#destroy
step_one POST /step_one(.:format) step_ones#create
new_step_one GET /step_one/new(.:format) step_ones#new
edit_step_one GET /step_one/edit(.:format) step_ones#edit
GET /step_one(.:format) step_ones#show
PATCH /step_one(.:format) step_ones#update
PUT /step_one(.:format) step_ones#update
DELETE /step_one(.:format) step_ones#destroy
GET /users/:id(.:format) users#show
sign_up GET /sign_up(.:format) users#new
log_in GET /log_in(.:format) step_one#login
step_two GET /step_two(.:format) sessions#new
log_out GET /log_out(.:format) sessions#destroy
root GET / welcome#index
your problem is in the naming I think, resources :step_one assumes that you define the StepOnesController (plural), not StepOneController
So, you should either rename your controller or use resource :step_one route (without the s at the end)
It looks like Rails is expecting you to pass in an id to your step_one_path, so the form_for should look like this:
<%= form_for url: step_one_path(#user.id) do %>
Where #user.id would be the id of the user that the form is associated with. Hope that helps. If not, please include your routes.rb file.
<%= form_tag step_one_path do |f| %>
was the form tag necessary, i was going about it wrong
thanks for all the help