How does redirect work in rails? - ruby-on-rails

I don't have a /charge page. But I do have a subscription/charge page. I was wondering, how do I say, "if a user goes to /charge, redirect them to subscription/charge" using routes
No route matches [POST] "/charged"

You can modify your routes.rb file to point a url to a specific action
get '/charge', to: 'subscription#charge'
I'm using something similar to below in my application for post:
match '/charge' => 'subscription#charge', :via => [:post], :as => :subscription_charge

you can also write:
root 'subscription#charge'
and you'll be redirected to the charge page if accessing http://localhost:3000

Related

How to restrict the wild card route

get '/:company' => 'organizations#show', as: 'company_home'
I have this route, this is a way so that different companies registered to my application will login. As I have overridden all the devise related things. This is working fine until I have realized that for every route, this is getting applied
When I hit
get 'employee_dashboard' => 'dashboard#show'
The Parameters: {"company"=>"employee_dashboard"} are going to organizations#show
But I want it to hit dashboard#show how to get around with this?
Move the wildcard route after
get 'employee_dashboard' => 'dashboard#show'
or at the end of route file
get '/:company' => 'organizations#show', as: 'company_home'
so this route will be used only if no other route matches

Rails - Redirect URL with params (routes)

I'm trying to create a redirect for anyone that has parameters in a certain url, to the homepage. I currently have this in my routes:
match "/pr?campaign=#{'params[:campaign]'}" => redirect("/")
but it's not recognising it properly, if i put /pr?campaign=test into the url I get a page not found..
I'm not sure if I'm understanding your question, maybe try this:
match '/pr' => 'controller#action' # responds to no additional params
match '/pr/:campaign' => 'another_controller#action' # responds to another param.
So, /pr will be received by controller#action, but /pr/test will be received by another_controller#action and receive test as params[:campaign]
Edit (actual solution to this problem):
match '/pr' => redirect('/')

Custom url in ruby on rails

I know rails uses the controller action style urls like www.myapp.com/home/index for example
I would like to have a url like this on my rails app, www.myapp.com/my_page_here is this possible and if so how would I go about this?
You just use a get outside of any resources or namespace block in your routes.rb file:
get 'my_page_here ', :to => 'home#index'
Assuming you are using Rails 3+, do NOT use match. It can be dangerous, because if a page accepts data from a form, it should take POST requests. match would allow GET requests on an action with side-effects - which is NOT good.
Always use get, put, post or these variants where possible.
To get a path helper, try:
get 'my_page_here ', :to => 'home#index', :as => :my_page
That way, in your views, my_page_path will equal http://{domain}/my_page_here
you just need to make a routing rule to match that url
in this case it will be something like
match 'my_page_here' => 'your_controller#your_action'
your controller and action will specify the behavior of that page
so you could do
match 'my_page_here' => 'home#index'
or
get 'my_page_here', :to => 'home#index'
as suggested in other responses.
for index action in home controller if you have such a controller
see http://guides.rubyonrails.org/routing.html for more details
also see Ruby on Rails Routes - difference between get and match

Rails implement an API using Routes pointing to existing controller actions

I have a Rails app that does everything I need it to do via a HTML interface, now I'd like to bolt on an API providing access to bits of the functionality.
How would I do this selective forwarding of some API controller actions to another controller's actions using the Routes.rb?
I have tried the following:
My regular controller routes work fine using:
match 'stuff' => 'stuff#index'
get 'stuff/index'
get 'stuff/some_get_action'
post 'stuff/some_post_action'
But then when I try for my API:
match 'api' => 'api#index'
match 'api/some_get_action' => 'stuff#some_get_action', :via => :get
match 'api/some_post_action' => 'stuff#some_post_action', :via => :post
or...
match 'api' => 'api#index'
get 'api/some_get_action', :to => 'stuff#some_get_action'
post 'api/some_post_action', :to => 'stuff#some_post_action'
I get an error. When I navigate to /api/index to server a HTML page that contains forms for testing the API URLs the url_for raises a 'Routing error' exception saying 'No route matches...'.
You may want to include ':as => ' and define your route names that you may be using as your link paths.
get 'api/some_get_action' => 'stuff#some_get_action', :as => 'some_get_action'
and the link path in your index file will have 'some_get_action_path'. Not sure that 'match' or 'get' automatically resolves to a path name, which by setting ':as' it definitely will.
I like your idea for setting up this page for testing. I'm always doing it in the console, which is surely more difficult than simply clicking a link. Your links probably need to infer that they are :json requests, not :http.

Rails 3 Routing to Add String to Home URL

Using Rails 3 routing, I'd like all visitors to the home page
http//mysite.com/
to see the URL
http//mysite.com/mykeyword
in their browser URL.
In other words, all visits to the home page get redirected to
http//mysite.com/mykeyword
This is approximately the opposite of the typical route
root :to => "home#index"
or
match '/' => 'home#index'
where any controller name gets stripped off from the home page URL.
Instead I want every visit to the site home page to include "mykeyword" in the URL.
How?
Justin is correct, but I believe you also want a redirect. To do that:
match "/" => redirect("/mykeyword")
Try this:
match '/mykeyword' => "home#index"
You want visitors to still be on the homepage, but the url to say "www.mysite.com/mykeyword" right? If so, that should work.

Resources