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
Related
Im trying to learn to dry up my code a bit but have come across a issue,
Im scoping my routes with controller and path options
scope path: '/administrators', controller: :administrators do
get 'unverified' => :unverified
patch 'verify/:id' => :verify
get 'reported' => :reported
get 'ban_user' => :ban_user
patch 'execute_ban/:id' => :execute_ban
end
So this is what ive done so far, all the get links are working correctly...
but this is making the patch 'execute_ban/:id' => :execute_ban become a extension of ban user like this: (also the same with verify)
verified GET /administrators/unverified(.:format) administrators#unverifie
PATCH /administrators/verify/:id(.:format) administrators#verify
reported GET /administrators/reported(.:format) administrators#reported
ban_user GET /administrators/ban_user(.:format) administrators#ban_user
PATCH /administrators/execute_ban/:id(.:format) administrators#execute_ban
now ive changed my link_to route = link_to 'ban', ban_user_path(x.id), method: :patch
but its throwing an routing error saying no path matches.
Is there something im missing, any insight would aooreciated as always.
Thanks
Why not go for a more restful design that centers around the resource being modified?
Rails.application.routes.draw do
resources :users do
get :unverified, on: :collection
get :reported, on: :collection
get :ban
patch :ban, action: 'execute_ban'
end
end
You don't need different paths for the form and acting on a resource - use the HTTP verb instead.
Prefix Verb URI Pattern Controller#Action
unverified_users GET /users/unverified(.:format) users#unverified
reported_users GET /users/reported(.:format) users#reported
user_ban GET /users/:user_id/ban(.:format) users#ban
PATCH /users/:user_id/ban(.:format) users#execute_ban
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
If you are adding a separate controller just for authorization purposes it's an anti-pattern of sorts since you are just adding more complexity for something that should be handled by Pundit/CanCanCan.
Each user can create a number of blogs and, when they log in, they are presented with a list of their blogs and a button next to each as below:
= simple_form_for activate_blog_path(blog.id), method: :put do |f|
= hidden_field_tag :active, value: true
= f.button :submit
Even though the path exists in routes, I'm still getting this error message:
No route matches [PUT] "/"
routes.rb:
resources :users
resources :blogs do
member do
get :activate
put :activate
end
end
root 'pages#index'
rails routes:
Prefix Verb URI Pattern Controller#Action
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
activate_blog GET /blogs/:id/activate(.:format) blogs#activate
PUT /blogs/:id/activate(.:format) blogs#activate
blogs GET /blogs(.:format) blogs#index
POST /blogs(.:format) blogs#create
new_blog GET /blogs/new(.:format) blogs#new
edit_blog GET /blogs/:id/edit(.:format) blogs#edit
blog GET /blogs/:id(.:format) blogs#show
PATCH /blogs/:id(.:format) blogs#update
PUT /blogs/:id(.:format) blogs#update
DELETE /blogs/:id(.:format) blogs#destroy
root GET / pages#index
blogs_controller.rb:
def activate
#blog.active = true
#blog.save
redirect_to root_path
end
What am I doing wrong here?
simple_form_for expects an object (or record) to generate the form tag. To pass custom url and method to a form action you could use
= simple_form_for blog, url: activate_blog_path(blog.id), method: :put do |f|
= hidden_field_tag :active, value: true
= f.button :submit
You need to specify the action and controller as well as the method
= simple_form_for blog, url: url_for(action: :activate, controller: 'blogs'), method: :put do |f|
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.
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 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'