configuring rails3 route with multiple parameters - ruby-on-rails-3

What I have
match "/home/markread/:id" => "books#markread"
goes to
def markread
#mark params[:id] as read
end
What I want
What If I want to pass another parameter so that urls looks like
/home/markread/1/didread=read or /home/markread/1/didread=unread
so my method will change to
def marked
#mark params[:id] as params[:didread]
end
Question
what should my routes.rb look like for me to achieve this?

How about just changing to
match "home/markread/:id/used=:used" => "books#markread"

Give the route a name using the 'as' option and pass the optional parameters as many you want.
For example:
match "/home/markread/:id" => "books#markread", :as => 'markread'
This will give you helpers like, markread_path and markread_url. You can pass the parameters like markread_path(:id => 1, :other => 'value' ...)
You need to do the checks in the controller for that action whether a particular parameter is passed or not. Rails Doc.

In rails 4 you will have:
resources :home, only: :none do
get 'markread/:another', action: :markread, on: :member
end
GET /home/:id/markread/:another(.:format) /home#markread

Related

rails routes optional parameters types by :id or :name

Scenario:
I would like to have a url path where you could look someone up by an :id or :name:.
For user/5 or /user/tom would all point to the same user
routes.rb
controller 'user' do
get 'user/:id'
get 'user/:name'
end
test/routes/user_routes.rb
test "/user/:id" do
assert_routing "/user/5", :controller => "user", :action => "find_by_id", :id=>"5"
end
test "/user/:name" do
assert_routing "/user/tom", :controller => "user", :action => "find_by_name", :name=>"tom"
end
I am not exactly sure if this is the right design decision with URL paths.
Looking for guidance
I don't think what you are doing is going to work, because the routes you gave are ambiguous, because rails routes can't say that url like user/15 has name or id.
Of course there is a way to do this, by specifying regular expression. Since id will always be a number, we can have regular expression check for it
controller 'user' do
get 'user/:id', :id => /\d+/
get 'user/:name', :name => /[A-Za-z][\w\s]+/
end
The above statements put a constraint of regular expression. In your controller you can check like
if params[:id]
# Get by id
else
# Get by name
You can also do this by passsing get parameters and handling it in controller in the same way.
Thanks

root path for multiple controllers on rails routes

I have two resource controllers where I am using a slug to represent the ID. (friendly_id gem).
I am able to have the show path for one resource on the route but not for two at the same time. ie.
root :to => 'home#index'
match '/:id' => "properties#show"
match '/:id' => "contents#show"
Basically I want urls like,
# Content
domain.com/about-us
domain.com/terms
# Property
domain.com/unique-property-name
domain.com/another-unique-property-name
Whatever resource I put on top works. Is there a way to do this?
Thanks in advace if you can help.
This is untested, but try utilizing a constraint on your route.
root :to => 'home#index'
match '/:id', :to => "properties#show",
:constraints => lambda { |r| Property.find_by_id(r.params[:id]).present? }
match '/:id', :to => "contests#show",
:constraints => lambda { |r| Contest.find_by_id(r.params[:id]).present? }
Alternatively, you can create a separate class that responds to matches? instead of defining a lambda proc. (I recommend placing these classes into separate files that will autoload within your Rails app.)
# app/constraints/property_constraint.rb
class PropertyConstraint
def self.matches?(request)
property = Property.find_by_id(request.params[:id])
property.present?
end
end
# app/constraints/contest_constraint.rb
class ContestConstraint
def self.matches?(request)
contest = Contest.find_by_id(request.params[:id])
contest.present?
end
end
# config/routes.rb
root :to => 'home#index'
match '/:id', :to => "properties#show", :constraints => PropertyConstraint
match '/:id', :to => "contests#show", :constraints => ContestConstraint
Unfortunately this results in an extra DB query (once in the routes, and once more in your controller). If anyone has a suggestion on minimizing this, please share. :)
This Rails Engine does what you want:
Slug Engine at Github
Basically, the author's approach was to mount a Rails Engine inside his main app. This Engine incorporates both a controller for handling slugs that exist and a piece of middleware for filtering out and abstaining on slugs that don't exist.
He explains why he took this approach and other aborted solutions in a rather detailed and interesting blog post. This blog post and the slug engine source code should be enough detail for you to get your own code up and running, but that open-source engine seems to be exactly what you're looking for if you want a drop-in solution.
You can do it in a middleware
Detect slug in path
If Content with this slug exists - change request path to "contents/:id"
If Property with this slug exists - change request path to "properties/:id"
in your routing set:
match 'contents/:id' => "properties#show"
match 'properties/:id' => "contents#show"
You could write another controller which takes the id from the router and checks if the id belongs to properties or content and renders the appropriate view.
match '/:id' => "router#show"
The controller would do something like this:
def show
#property = Property.find(params[:id])
if #property then
render 'property/show'
else
#content = Content.find(params[:id])
render 'content/show
end
end
Havn't tested this code, but this idea should work.
I would suggest that you do this in a more RESTful way, if possible. Basically, you have two different resources and you should separate them:
match 'properties/:id' => "properties#show"
match 'contents/:id' => "contents#show"
This will give you many advantages down the road. One immediate benefit is that you can avoid clashes between ids for properties and content. (Note that friendly_id will not help you with inter-model slug clashes in your original scheme.)

Questions about rails3 routes

I'm upgrading my app to rails 3, and I am a bit confused about some of the routes. The resourceful ones are easy enough, but how can I set a generic rule for all actions in a specific controller. I tried something like this:
get 'custom/:action/' => {:controller => :custom}
But that didn't work. It seems the new format is "controller#action", but how can I specify the action to be variable?
Also, other than using named routes or resources, is it possible to do shorthand notation to name routes in a specific controller?
i.e. rather than:
get '/tasks', :controller => :home, :action => :tasks, :as => 'tasks_home'
get '/accounts', :controller => :home, :action => :accounts, :as => 'accounts_home'
is it possible to do something a little cleaner, like:
controller => :home do
get :tasks
get :accounts
end
And that would automatically created the named routes?
You can use action as a variable like this:
resource :custom do
match ':action'
end
This will generate
/custom/:action(.:format) customs#:action
custom POST /custom(.:format) customs#create
new_custom GET /custom/new(.:format) customs#new
edit_custom GET /custom/edit(.:format) customs#edit
GET /custom(.:format) customs#show
PUT /custom(.:format) customs#update
DELETE /custom(.:format) customs#destroy
So it will handle your action as a variable URL-s and will add some default CRUD actions as well.
Note that the controller name here is in plural. If you would like to use a route for a controller which name is in singular, use resources instead of resource.
The answer to the second question is almost identical to the first one, use resource:
resource :home do
get :tasks
get :accounts
end
generates:
tasks_home GET /home/tasks(.:format) homes#tasks
accounts_home GET /home/accounts(.:format) homes#accounts
home POST /home(.:format) homes#create
new_home GET /home/new(.:format) homes#new
edit_home GET /home/edit(.:format) homes#edit
GET /home(.:format) homes#show
PUT /home(.:format) homes#update
DELETE /home(.:format) homes#destroy
Note that the matched controller names are in plural again, because of the convention.
Looks like this is related to the persisted field being set to false on nested ActiveResource objects: https://github.com/rails/rails/pull/3107

Rails 3 Routing: Using 2 dynamic segments in path for one model

What I am trying to achieve is something similar to Github's way for routes. E.g. I have a project with the name 'question' results in the URL /hjuskewycz/question. So my goal is to have routes where the first segment is the username and the second the project's name.
I tried a couple of different approaches, this is the one I am stuck with right now:
scope ":username" do
resources :projects, :path => "" do
resources :pictures
end
end
Using
project_path :username => project.owner.username, :id => project.to_param
works as expected. However, it's tedious to always specify the username although it's always the owner's username. I would very much prefer
project_path(:id => project.to_param)
I know about default_url_options and url_for and I digged in the code. However, polymorphic_url doesn't use default_url_options.
I tried in routes.rb:
resources :projects, :path => "", :defaults => {:username => Proc.new { "just_testing" }}
since you can use a proc for constrains, but haven't got it working either.
I tried in project.rb
def to_param
"#{owner.username"/#{project.title}"
end
I spent already too much time on this problem and my current approach uses a convenience method to add the :username parameter. Nevertheless, I think using this method all over the place just to add an entry stinks (bad code smell). I wonder if there is a more elegant solution to this problem.
I think you should not make things complicated here, just use something like this:
In Routes.rb
match ':username/:projectname/' => 'projects#show_project' , :as => :show_project
and in project_controller, just define this
def show_project
#user =User.find_by_username(params[:username])
#project =Project.find_by_slug(params[:projectname])
end
Simpler is better, it saves time and easy to understand for others
You want to do something like this in your controller:
before_filter :set_username
def set_username
Rails.application.routes.default_url_options[:username] = #user.name
end

Rails routes with multiple identifiers?

I'm trying to set up routes in Rails 3 that look like:
/items/:category/:name/
It's pretty easy to do a match to set this up, and then generate the URL with the following:
item_path(:category => #item.category, :name => #item.name)
But is there any way to set it up so that item_path #item and form_for #item will work automatically, so I don't have to pass the category every time?
Thanks!
Not really no. I would suggest defining a to_params (note the 's') method on Item as follows:
def to_params
{:category => category, :name => name}
end
And then calling it like so item_path(#item.to_params). If you hack things to default to this I can guarantee you'll run into situations in which you don't want it.