Matching show route not generating a path? - ruby-on-rails

I have a Model called UserPrice and when I make the show route match this:
match "/:id/:product_name/:purchase_date/:price", :to => "user_prices#show"
It generates no new route path to use nor does it change it in the view. Why doesn't it do this? How would I get it to do this?

You should use the as => [name] syntax:
match "/:id/:product_name/:purchase_date/:price", :to => "user_prices#show", :as => :show
will create show_path and show_url (see http://guides.rubyonrails.org/routing.html#naming-routes)

Related

Rails route to get a resource using query string

I would like to have a custom route querystring based, to access a specified resource. For example:
/opportunities/rent/san-miguel-de-tucuman?id=45045
That route should map to the action OpportunitiesController#show, with the resource id 45045.
Thanks in advance!!!
Updated
This are my current routes:
get 'oportunidades/alquiler/san-miguel-de-tucuman/:id', to: "opportunities#show"
get 'oportunidades/alquiler/san-miguel-de-tucuman', to: "opportunities#index"
So, if I navigate to the /oportunidades/alquiler/san-miguel-de-tucuman?id=123456 route, it go to the Opportunities#index action.
P/S: sorry, I forget to mention that I have a similar route for the index action.
Make your custom routes as:
resources: opportunities, except: :show
get '/opportunities/rent/san-miguel-de-tucuman/:id' => 'opportunities#show', :as => 'opportunities_show'
and pass your 'id' as opportunities_show_path(id)
EDIT
Change your routes as:
get 'oportunidades/alquiler/san-miguel-de-tucuman/:id' => "opportunities#show", :as => 'opportunities_show'
get 'oportunidades/alquiler/san-miguel-de-tucuman' => "opportunities#index", :as => "opportunities_index"
Now when you want to access your show page just use opportunities_show_path(:id =>123456 )
And for index page use opportunities_index_path
Use this
match '/opportunities/rent/san-miguel-de-tucuman/:id', :to => 'opportunities#show', :via => :get
and pass a object to the path so created. Eg:-
something_path(#object), here #object is object that with id which will be passed in routes
Option 1
Query string parameter
// /opportunities/rent/san-miguel-de-tucuman?id=45045
match '/opportunities/rent/san-miguel-de-tucuman', :to => 'opportunities#show', :as => "show_opportunity", :via => :get
Option 2
Add id like new parameter. More friendly.
// /opportunities/rent/san-miguel-de-tucuman/45045
match '/opportunities/rent/san-miguel-de-tucuman/:id', :to => 'opportunities#show', :as => "show_opportunity", :via => :get
In both cases, you need to call the route like this:
show_opportunity_url(:id => 45045)
In your controller you will get the id in params[:id]

More Dynamic Routes?

I have this site where I want to be able to export all the data using CSV. There is a controller called "dataexport" and it has a method for each model. In my routes.rb file, I have this:
match "export_checkouts", :to => "dataexport/checkouts_csv"
match "export_committees", :to => "dataexport/committees_csv"
match "export_libitems", :to => "dataexport/libitems_csv"
match "export_locations", :to => "dataexport/locations_csv"
match "export_logs", :to => "dataexport/logs_csv"
match "export_patrons", :to => "dataexport/patrons_csv"
match "export_products", :to => "dataexport/products_csv"
match "export_questions", :to => "dataexport/questions_csv"
match "export_reasons", :to => "dataexport/reasons_csv"
match "export_roles", :to => "dataexport/roles_csv"
match "export_sales", :to => "dataexport/sales_csv"
match "export_shifts", :to => "dataexport/shifts_csv"
match "export_tasks", :to => "dataexport/tasks_csv"
match "export_tickets", :to => "dataexport/tickets_csv"
match "export_types", :to => "dataexport/types_csv"
match "export_users", :to => "dataexport/users_csv"
match "export_visitors", :to => "dataexport/visitors_csv"
match "export_years", :to => "dataexport/years_csv"
Is there a more dynamic way of doing this? This definitely goes against the "DRY" paradigm and was wondering if anyone could help me with this. I was thinking that you could just do this in one line by replacing the model names with a variable but I'm not quite sure how to go about doing this.
Why not just:
match "export/:model", :to => "dataexport/export_csv"
and use params[:model] to get the correct Model, then direct the dataexport controller export_csv method to ask the model for the data in CSV format like:
class DataexportController do
def export_csv
params[:model].constantize.export_csv
end
end
You could try this:
%w(checkouts committees).each do |model|
match "export_#{model}", :to => "dataexport/#{model}_csv"
end
Obviously fill out the array with all of the models you need this for.
However, whilst this cuts down on the code, you are still polluting your routes. You should consider that there might be a more Rails-way of doing this.
One thing Rails has support for is responding to different formats in controllers. So if a JSON format is requested by the browser, a JSON file is provided for by Rails (as long as you write the code for it). It sounds to me like you could just do the same thing with a CSV format.
What you are defining as "export" is really just the index method on a normal controller. It's just that rather than displaying the data as HTML, you are displaying it as CSV. I haven't really looked into this myself and so I'm not sure exactly how you would go about doing it. Something like this:
class FooController < ApplicationController
def index
respond_to do |format|
format.html #This will load your standard html index view
format.csv { #CSV stuff goes here. Perhaps you can get it to load app/views/foo/index.csv.erb somehow }
end
end
There is some discussion on this here: http://weblog.rubyonrails.org/2006/12/19/using-custom-mime-types

How do you have the create Action route to an alternative model name?

Routes.rb
resources :recovers match ':checkedin'
=> 'recovers#index' match ':checkin/:id' => 'recovers#show'
When a new record is created Rails routes the user to /recovers, I would like the user to be routed to /checkedin how do I do this?
You could do the following:
routes.db:
resources :recovers
match 'checkedin' => 'recovers#index', :as => 'checkedin'
match 'checkin/:id' => 'recovers#show'
recovers_controller.rb:
in the create method:
redirect_to checkedin_path

Rails 3: Replace /users/username/something for /username/something

I already have a route to match /username for the users show page. However, when I add another action, for example: followers and following as below:
resources :users, :only => [:show] do
get :following, :followers
end
I get the URL /users/username/following instead of /username/following.
How can I make all the /users/username URL's be matched as /username/etc.. ?
Here's my /username route:
match '/:id' => 'users#show', :constraints => { :id => /[a-zA-Z0-9\-_]*/ }, :as => "user_profile"
thank you.
Edit:
I have fixed this by adding
match '/:id/following' => 'users#show/following', :as => "user_following"
match '/:id/followed' => 'users#show/following', :as => "user_followers"
But I'm not sure if that's the best solution. What I'd really want is a default route that would match all /:id/:action to the appropriate action omitting the /users.
I have fixed this by adding
match '/:id/following' => 'users#show/following', :as => "user_following"
match '/:id/followed' => 'users#show/following', :as => "user_followers"
But I'm not sure if that's the best solution. What I'd really want is a default route that would match all /:id/:action to the appropriate action omitting the /users.
Near the bottom
match ':id/:action', :controller => :users

(Rails 3) Is it possible to hardcode ids in a named route?

As far as I know, the following is the encouraged way to create a simple named route in Rails 3:
match 'sign-in' => 'sessions#create', :as => :sign_in
Is there a clean way to hardcode an id (or any parameter) in a named route? For a silly example:
match 'first-user' => 'users#show', :as => :first_user, :id => 1
Did you try that? It just works like that.

Resources