/session instead of /login when login attempt failed - ruby-on-rails-3

sessions_controller.rb
def create
if user = User.authenticate(params[:login], params[:password])
session[:user_id] = user.id
redirect_to posts_path
else
render :action => 'new'
end
end
routes.rb
get "sessions/create"
get "sessions/destroy"
get "sessions/new"
resources :posts
resource :session
resources :users
match '/login', :to => 'sessions#new', :as => 'login'
match '/logout', :to => 'sessions#destroy', :as => 'logout'
Is it possible to keep the /login url after the render :action => "new" ???
thanks.

redirect_to '/login' does not keep the post information like render 'new' does.
I'm not completely happy with this solution, but this is what I have done:
resource :session, :only => [:create, :new, :destroy],
:path_names => { :new => 'login' }
Which gives you the following routes:
session POST /session(.:format) {:action=>"create", :controller=>"sessions"}
new_session GET /session/login(.:format) {:action=>"new", :controller=>"sessions"}
DELETE /session(.:format) {:action=>"destroy", :controller=>"sessions"}

The easy solution would be to simply change render :action => 'new' to redirect_to '/login'. I'm not amazingly fond of this, but it should solve the problem for you.

Related

cleaning up rails 3 routes

I am using devise with few changes to the controllers and have the following routes defined.
devise_for :users, skip: [:registrations, :sessions]
devise_scope :user do
resource :registration,
only: [:new, :create, :edit, :update],
path: 'users',
path_names: { new: 'sign-up' },
controller: 'registrations',
as: :user_registration do
get :cancel
end
get 'sign-in' => 'devise/sessions#new', :as => :new_user_session
post 'sign-in' => 'devise/sessions#create', :as => :user_session
delete 'sign-out' => 'devise/sessions#destroy', :as => :destroy_user_session
get "sign-up" => "registrations#new", :as => :new_user_registration
end
resulting in the following duplicate routes:
new_user_registration GET /users/sign-up(.:format) registrations#new
new_user_registration GET /sign-up(.:format) registrations#new
How do i clean my route file that the duplication does not occur.
not sure, postet as answer for format:
I think that the redundancy cames from here:
devise_scope :user do
resource :registration,
only: [:new, :create, :edit, :update],
path: 'users',
path_names: new: 'sign-up' ,
this makes the
new_user_registration GET /users/sign-up(.:format) registrations#new
and
get "sign-up" => "registrations#new", :as => :new_user_registration
adds
new_user_registration GET /sign-up(.:format) registrations#new
so leave one of them

Devise custom user root is not working

After a user signs in, I want them to be redirected to /users/2 or users/44, like a normal resource. (That way an admin can easily view any user's profile page just by knowing their id.)
I have no idea what is going on in my routes file. When a user logs in, I can see in the logs that it authenticates properly, and that my application_controller's after_sign_in_path_for is called.
But this is the error that I am getting when I try to redirect_to user_path(current_user).
No route matches {:action=>"show", :controller=>"users", :user_id=>11}
My config/routes.rb
devise_for :users, :path_names => {:sign_in => 'signin', :sign_out => 'signout', :sign_up => 'signup' }
devise_scope :user do
get "signin", :to => "devise/sessions#new"
get "signout", :to => "devise/sessions#destroy", via: :delete
get "signup", :to => "devise/registrations#new"
end
resources :users do
member do
get 'settings', to: 'users#edit'
end
end
My application_controller.rb
class ApplicationController < ActionController::Base
...
def after_sign_in_path_for(resource)
redirect_to user_path(:user_id=>current_user.id)
end
end
Shouldn't the resource :users block take care of creating a user show path? I do have a show method in my users_controller.rb. Please help, I've been trying to get this working for 2 days now.
My problem was that I was redirecting in the after_sign_in_path(resource) method instead of just returning the path:
def after_sign_in_path_for(resource)
return user_path(:user_id=>current_user.id)
end
is the correct way to do this.

Rails 3 Unknown Action even though its define in my routes

I have an app that I am working on that directs users to a "bundle" page when purchasing one product so that they have the opportunity to add another product to "bundle" their purchase for a discount.
Here is my routes:
resources :orders, :path_names => { :new => 'checkout' }
match "/orders/bundle" => "orders#bundle", :as => 'bundle_order'
match "/orders/add_product" => "orders#add_product", :as => 'add_product'
Here is my Controller#Action
def bundle
op_client = Client.find_by_name(opposite_client(current_client))
#product = Product.find_by_client_id_and_type_and_status(op_client.id, "subscription", "Active")
respond_with #product
end
For some reason when I redirect_to this method, I receive this error:
Unknown action
The action 'show' could not be found for OrdersController
I don't have a show method in my OrdersController cause I don't need it. Why would I be seeing this issue?
This error could be caused by two diferent things.
first:
are you using something like: <%= link_to #order_object %> ?? if you are, this is the problem.
second:
on routes.rb change this line:
resources :orders, :path_names => { :new => 'checkout' }
to
resources :orders, :path_names => { :new => 'checkout' }, :except => [:show]
this should work. if not, please give more details about the code you are using to do the redirect_to

Links to resources with friendly URLs

I'm trying to write a blog in Rails 3, so I have posts. I want to make nice routes for the posts like this: posts/year/month/day/post-title. So I overrided to_param in model/post.rb and use friendly_id for title:
extend FriendlyId
friendly_id :title, :use => :slugged
def to_param
"#{year}/#{month}/#{day}/#{title.parameterize}"
end
And I added this to routes.rb:
resources :posts do
collection do
match ":year/:month/:day/:id", :action => "show"
end
member do
post 'comment'
delete 'comment/:comment_id', :action => 'destroy_comment', :as => 'destroy_comment'
end
end
In show this works:
<%= link_to image_tag("/images/edit_icon.png"),
{ :controller => 'posts', :action => 'edit' }, :id => #post %>
But in index it says
routing error:
No route matches {:action=>"edit", :controller=>"posts"}.
I'm new to Rails and I haven't managed to find out what causes this error. Can anyone help me figure out what I do wrong?

Rails 3 Correctly routing the destroy action for a session

I am refactoring my access_controller into a sessions_controller and can't seem to get my destroy action working properly.
Logging in seems to work fine, but I am unable to log out of a session. Here is the link I have for logging out:
<%= link_to("Logout", :controller => "sessions", :action => 'destroy') %>
routes.rb
resources :sessions
sessions_controller.rb
class SessionsController < ApplicationController
def new
end
def create
...
end
def destroy
session[:user_id] = nil
flash[:notice] = "You are now logged out"
redirect_to root_url
end
end
When I click "Logout" I get redirected to "/sessions/destroy" with a message of "The action 'show' could not be found for SessionsController". The destroy actions seems to want an id, but I don't need to pass in an id, I just want to run the action.
Ah, I found the answer here: http://railscasts.com/episodes/250-authentication-from-scratch
I need to set up my routes as follows:
get "log_out" => "sessions#destroy", :as => "log_out"
get "log_in" => "sessions#new", :as => "log_in"
resources :sessions