Adding member or alias to existing path in Rails 3 - ruby-on-rails

For the jobs -> apply action I want to "alias" the "create" action.
In rake routes it would look something like this:
https://gist.github.com/YOUConsulting/19b404759757898a6f4f#file-rake_routes-rb
I've tried to do it like this but I think this is not exactly what I'm looking for:
resources :jobs, :only => [:show, :index, :create] do
resources :apply, :only => [:index, :create] do
member do
post :completed
end
end
resources :share, :only => [:index, :create]
end
In human words:
When the user submits the page located at "/jobs/<job_id_here>/apply" (the index view) the result page (the create view) should be "/jobs/<job_id_here>/apply/completed" instead of "/jobs/<job_id_here>/apply/" .
Reason:
When we track users via Google Analytics we can't see if they submitted the form successfully since there is no difference between "/apply" and "/apply" .

Hm, what about using a named route as path
Add a line at the end of your routes.rb
post '/jobs/:job_id/apply/completed', to: apply#create as: apply_job
And then in your new.html.haml
form_for :job, apply_job_path
This should call the create action of your apply controller.
Instead of apply you can use of course any name you want.

Related

Ruby on Rails - Path variable without using resources route

I have a controller, let's just call it FruitsController, that grabs all of the fruit and sends it to the index view. In the view, I want to show links to the individual pages for those fruits. I'm using the format:
<% #fruits.each do |fruit| %>
<%= link_to fruit.name, fruit_path(fruit) %>
<% end %>
And this works great when I have the route resources :fruits, but I don't want routes for deleting, saving, and updating, so I don't want to use resources. But when I just do individual routes for showing all and individual fruits, I get the error fruit_path function is not defined, and when I use the function fruits_path it works but it just appends a period to the path like /fruits.1. How can I use the fruit_path function without using resources? Thanks.
There are a number of ways you could do this; in your config/routes.rb, any of the following should work:
resources :fruits, only: :show
resources :fruits, except: [:index, :edit, :destroy, :update] # etc
get 'fruits/:id', to: 'fruits#show', as: :fruit
scope controller: :fruits do
get 'fruits/:id' => :show, as: :fruit
end
You're not limited to just resources, you can customize and create your own routes, for example:
get '/:username/photos', to: 'users#show', as: 'collage'
to: means controller/action, in this case users is the controller and show is the action.
as: creates a path for you 'collage_path'
you can find good info regarding routing =>http://guides.rubyonrails.org/routing.html

Add URL parameters on successful create method

Case:
When I submit my form that is located on /jobs/test-lune-f/apply?locale=fr the create method gets launched and does some checks.
The code will either run trough the whole create method and go the the create.html.erb page or will do return render :index.
Problem:
The problem is that the create.html.erb has the same URL /jobs/test-lune-f/apply?locale=fr as the index.html.erb page.
Question:
When the create method runs completely ok, is it possible to an URL parameter like #success to it?
I've tried to do this via the addition of :anchor => "success" to the form_for but it also get's added when the method returns via return render :index.
Goal:
To be able to add URL parameters when the form get's submitted successfully.
CODE:
Routes:
resources :jobs, :only => [:show, :index, :create] do
resources :apply, :only => [:index, :create]
resources :share, :only => [:index, :create]
end
resources :apply, :only => :index do
collection { get 'add'}
end
You have to change the location before rendering the page:
location=location()+(location().include?'?' '&' : '?') +"status=success"
More info about api location.
I was trying to do it with Rails but as mentioned the most easy fix is in jQuery.
<script>
$(document).ready(function() {
location.hash = "success";
});
</script>

Can I get a controller to show up in the url under a different name?

I have a controller called "Pages". Can I make it show up in the url bar under a different name? For example, when I render the 'show' template, it shows up under this url: localhost:3000/pages/:id. Could I make it show up as localhost:3000/people/:id? I only care about the 'show' url; the other urls aren't that important.
routes.rb
get "pages/results"
get "pages/index" => "pages#index", as: "index_page"
resources :pages do
resources :categories
end
Add this your routes:
get '/people/:id', to: 'pages#show'
And remove the old show route from the resource:
resources :pages, only: [:index, :new, :create, :edit, :update, :delete] do
resources :categories
end
See: Rails guides about rounting
You could use like this:
get '/pages/:id' => "pages#show", path: 'people/:id'
This way you can access particular show page in the browser.
Hope this helps you.

How to rename index path in routes file?

How to rename index path only?
routes.rb
resources :tasks, :except => [:create] do
collection do
..............
end
member do
.............
end
end
instead of /tasks in URL I need /trigger or even /tasks/trigger will do, im on rails3.
I tried
1.
collection do
'/', to: 'tasks#trigger'
end
and
2.
resources :tasks, :except => [:create] do
get '/task', to: 'task#trigger', as: task_index
end
both throw up errors.
any ideas?
For URL like '/trigger' add to routes.rb:
get 'trigger' => 'tasks#index'
For URL like '/tasks/trigger' modify routes.rb:
resources :tasks, :except => [:create] do
collection do
get 'trigger' => 'tasks#index'
end
end
If your index method in task's controller named as 'trigger, 'tasks#index' is replaced by 'tasks#trigger'
If you want to make your own index route then you need to do two things
1- you need to disable index path created by resources
2- Define your own path whatever you want.
As per your requirement you want to make index route as '/trigger'.
Following is the solution.
get 'trigger' => 'tasks#index'
resources :tasks, :except => [:create, :index] do
collection do
..............
end
member do
.............
end
end
Just restraint index route from being generated in your resources call:
resources :tasks, :except => [:create, :index]
And then do your own custom route. For '/trigger':
get 'trigger', to: 'task#trigger', as: 'trigger

How can I specify two actions mapped to the DELETE verb in a rails resource?

I have an api tokens controller based on Matteo Melanis blog post. I'd like to add two custom actions register and unregister to the controller, and so the route that looked like this
namespace :api do
namespace :v1 do
resources :tokens,:only => [:create, :destroy]
end
end
has now become this
namespace :api do
namespace :v1 do
resources :tokens do
put 'register', on: :member, as: :register
delete 'unregister', on: :member, as: :unregister
end
end
end
This is the only way I've found that doesn't let unregister suppress the CRUD destroy action, associated with the DELETE verb. I tried to do
resources :tokens, :only => [:create, :destroy, :register, :unregister] do
in the above code, as well as defining resources :tokens,:only => [:create, :destroy] in parallel to the block. Yet, I either get the undesirable all CRUD + custom actions, or one of the custom actions overriding a CRUD action.
In short, I'd like to end up with
register_api_v1_token PUT /api/v1/tokens/:id/register(.:format) api/v1/tokens#register
unregister_api_v1_token DELETE /api/v1/tokens/:id/unregister(.:format) api/v1/tokens#unregister
api_v1_tokens GET /api/v1/tokens(.:format) api/v1/tokens#index
POST /api/v1/tokens(.:format) api/v1/tokens#create
DELETE /api/v1/tokens/:id(.:format) api/v1/tokens#destroy
Is this possible, and if yes: how can I make it so?
This should work:
resources :tokens, only: [:create, :destroy] do
member do
put 'register'
delete 'unregister'
end
end

Resources