2 pages using the same url using rails routes - ruby-on-rails

Im trying make a login page for my rails application that looks like "www.domain.com" and when you login you still are still located at the domain "www.domain.com". Is there a way that I can map 2 different actions to the same url using routes. Twitter does it this way, you log in at twitter.com and after you are logged in you are still located at twitter.com.

You can't do this by simply modifying the routes, but you can do some kind of conditional statement in your controller.
def index
if logged_in
render :action => 'show'
else
render :action => 'new'
end
end
def show
...
end
def new
...
end
There are going to be numerous ways to do this, of course.

After successful login redirect to the root URL.
routes.rb
map.resources :landings
# let's assume that, home page corresponds to landings/index
map.root :controller => "landings", :action => "index"
UserSessionsController
def create
#user_session = UserSession.new(params[:user_session])
if #user_session.save
redirect_to root_url
else
render :action => :new
end
end

Related

redirect_to in controller only routes to absolute url

I have a little check in my controller after login in my home_controller.
before_filter :authorize_admin, only: :index
def authorize_admin
redirect_to '/admin/index' if current_user.admin?
#redirects to admin controller if admin? true
end
Annoying thing is it only works when I put in the absolute path. I tried redirect_to 'admin_index' and redirect_to 'admin#index'. But both ends in an error.
This is the log from the server:
Redirected to http://localhost:3000admin_index
And for the controller#action way its
Redirected to http://localhost:3000admin#index
Its obvious what happens, but its kinda annoying to do the redirect by absolute url.
Any idea or suggestions?
Try with
redirect_to {:controller => 'admin', :action => 'index'} if current_user.admin?
Or easier, if you have defined a named route
redirect_to admin_index_path if current_user.admin?

How do I define a custom URL for a form confirmation page?

I am creating a basic product landing page with Rails in which users can enter their email address to be notified when the product launches. (Yes, there are services/gems etc that could do this for me, but I am new to programming and want to build it myself to learn rails.)
On successful submit of the form, I would like to redirect to a custom '/thanks' page in which I thank users for their interest in the product (and also encourage them to complete a short survey.)
Currently, successful submits are displayed at "/invites/:id/" eg "invites/3" which I do not want since it exposes the number of invites that have been submitted. I would like to instead redirect all successful submits to a "/thanks" page.
I have attempted to research "rails custom URLs" but have not been able to find anything that works. The closest I was able to find was this Stackoverflow post on how to redirect with custom routes but did not fully understand the solution being recommended. I have also tried reading the Rails Guide on Routes but am new to this and did not see anything that I understood to allow for creating a custom URL.
I have placed my thanks message which I would like displayed on successful form submit in "views/invites/show.html.haml"
My Routes file
resources :invites
root :to => 'invites#new'
I tried inserting in routes.rb:
post "/:thanks" => "invites#show", :as => :thanks
But I don't know if this would work or how I would tell the controller to redirect to :thanks
My controller (basically vanilla rails, only relevant actions included here):
def show
#invite = Invite.find(params[:id])
show_path = "/thanks"
respond_to do |format|
format.html # show.html.erb
format.json { render json: #invite }
end
end
# GET /invites/new
# GET /invites/new.json
def new
#invite = Invite.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: #invite }
end
end
# POST /invites
# POST /invites.json
def create
#invite = Invite.new(params[:invite])
respond_to do |format|
if #invite.save
format.html { redirect_to #invite }
#format.js { render :action => 'create_success' }
format.json { render json: #invite, status: :created, location: #invite }
else
format.html { render action: "new" }
#format.js { render :action => 'create_fail' }
format.json { render json: #invite.errors, status: :unprocessable_entity }
end
end
end
It would seem as if creating a standard URL for displaying a confirmation would be relatively straightforward. Any advice on how to achieve this would be appreciated.
I guess you want to redirect after your create action, which is executed when the form is submitted.
Just add redirect_to in the following way:
def create
#invite = Invite.new(params[:invite])
if #invite.save
...
redirect_to '/thanks'
else
...
redirect_to new_invite_path # if you want to return to the form submission page on error
end
end
I omitted some of the code for brevity.
In your routes add:
get '/thanks', to: "invites#thanks"
Add the thanks action to your invites controller:
def thanks
# something here if needed
end
And create a thanks.html.erb page in app/views/invites.
I would do get "/thanks" => "invites#thanks" in routes.rb and then add this in your controller:
def thanks
end
Then add a file app/views/invites/thanks.html.erb with your thank-you content.
You could create a route like this:
resources :invites do
collection do
get 'thanks'
end
end
This will also create a path helper called thanks_invites_path.
It will be at the invites/thanks path, but if you want it to be on/thanks, you could just do as Jason mentioned:
get "/thanks" => "invites#thanks", :as => :thanks
The as part will generate a helper to access that page: thanks_path.
You would need a extra action in the controller called thanks, and put whatever info you need inside, and also you will need a additional view called thanks.html.erb
Since you want everybody to go to that page after a successful submit, in your create action you would have:
format.html { redirect_to thanks_invites_path} (or thanks_path), what ever you choose, when you name the route you can check it with rake routes if it's okay, and whatever rake routes says, just add _path at the end.

how to route to a different page in ruby on rails

I have a session controller like
def create
user = User.find_by_email(params[:session][:email])
if user && user.authenticate(params[:session][:password])
sign_in user
redirect_to user
else
flash.now[:error] = 'Invalid email/password combination' # Not quite right!
render 'new'
end
end
if you notice on successful sign in it does redirect_to user which is the show action in the User controller.
But say instead of that I have to go to the new action of user controller then how do i do that?
redirect_to user is just a shortcut for redirect_to url_for(user), which generates the url for a given resource (url_for) and then redirects to it.
If you want to redirect to another url, you can use the path helper.
redirect_to new_user_path
You could also use the url_for helper to generate an url.
redirect_to url_for(:controller => "users", :action => "new")
Which can be shortened to
redirect_to :controller => "users", :action => :new
You can even specify an url directly (redirect_to '/users/new') - which I would not recommend, as you can't change your routing later without changing all the urls.
The docs
As you can see, there are many ways to specify an url to redirect_to in rails. Take a look at the documentation for all of them
To render an action from another controller:
render 'users/new'
See: http://guides.rubyonrails.org/layouts_and_rendering.html#using-render

Rails 3 'new' action as the index page due to a form

I have a small app where the index page is a form that instructs the user for their email, I have changed my route so that the root_path is the 'new' action which renders my form:
Oa::Application.routes.draw do
resources :signups
match "/confirm", :to => "pages#confirm"
root :to => 'signups#new'
end
This is working fine, when I submit the form it is working fine. When I submit on the root page and I cause a validation error the address bar has the url localhost:3000/signups and shows me my validation errors, which is also good but if I were to manually visit http://localhost:3000/signups it gives me an error "The action 'index' could not be found for SignupsController". Would it be ok if I created the 'index' action and redirect_to root_path so that I don't receive the "The action 'index' could not be found for SignupsController" if I were to access http://localhost:3000/signups directly? Is this the proper way to do this?
class SignupsController < ApplicationController
def index
redirect_to root_path
end
def new
#signup = Signup.new
end
def create
#signup = Signup.new(params[:signup])
if #signup.save
UserMailer.registration_confirmation(#signup).deliver
flash[:notice] = "Signup created successfully."
redirect_to confirm_path
else
render :action => "new"
end
end
end
Thanks!
J
Do something like this:
post '/' => 'signups#create'
root to: 'signups#new'
Do not use resources :signups
The reason that http://localhost:3000/signups gives me an error "The action 'index' could not be found for SignupsController" is because:
The first 'matched' routes is used.
So all of this:
match "/confirm", :to => "pages#confirm"
root :to => 'signups#new'
is ignored by the /signups URL because IT gets dealt with by
resources :signups
which creates all the routes - index, show, create, new, edit, update, destroy
and when 'signups' is used it implies the signups index action.
Try removing it and/or reading up on RESTful routes in rails and apply that.

Weird Rails URL issue when rendering a new action

I am rendering a new action but somehow getting the "index" URL. To be more specific, my create action looks like this:
class ListingsController < ApplicationController
def create
#listing = Listing.new(params[:listing])
#listing.user = #current_user
if #listing.save
redirect_to #listing
else
flash[:error] = "There were errors"
render :action => "new"
end
end
end
When there are errors, I get the "new" action but my URL is the index URL - http://domain.com/listings
Anyone know why this would happen? My routes file is fairly standard:
map.connect 'listings/send_message', :controller => 'listings', :action => 'send_message'
map.resources :listings
map.root :controller => "listings"
map.connect ':controller/:action/:id'
map.connect ':controller/:action/:id.:format'
when you render you just get the content that will be returned to the browser as the response body. On Rendering your url is not get changed.
Best example of that create a scaffold application.so when you submit the form on the new and error occurs your 'new.html.erb' is displayed but your url shows domain_name/controller_name/create
Hope that helps :)

Resources