Rails - Redirect URL with params (routes) - ruby-on-rails

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('/')

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

How does redirect work in 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

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.1 Routes with fixed structured params

I have an sms gateway which pushes me get-requests if a user replys an received sms.
# gateway pushes following fixed style get-params
# to my server/reply_from_gateway-action: ?id=123456&answer=Test
# => http://myserver.aaa/reply_from_gateway?id=123456&answer=Test
And now I want to add following route, since the sms gateway has a defined get parameter structure:
get "deactivate_via_sms?id=:id&answer=:answer" => "reminders#deactivate_via_sms"
:as => "deactivate_via_sms"
But that doesn't work, can you help me?
You can pull CGI-style parameters out of params by hand in your controller, you don't need (or want) them in the route:
get "deactivate_via_sms" => "reminders#deactivate_via_sms", :as => "deactivate_via_sms"
and then in RemindersController#deactivate_via_sms:
def deactivate_via_sms
id = params[:id]
answer = params[:answer]
#...
end
You can pull CGI-style parameters out of params by hand in your controller, you don't need (or want) them in the route
but in this case you can't use helpers such as
deactivate_via_sms_path(id,answer)
or you can use this code for creating helper
get "deactivate_via_sms?id=:id&answer=:answer" => "reminders#deactivate_via_sms"
:as => "deactivate_via_sms"
but your routing will fail
I resolved this issue by changing "?" to "/" in route
get "deactivate_via_sms/id=:id&answer=:answer" => "reminders#deactivate_via_sms"
:as => "deactivate_via_sms"
routing works and helper method also works fine

Resources