Is it possible to set up a routing parameter for both :id and :token?
There are ample resources out there on how to change the default route params:
resources :events, params: :token
def to_param
token
end
However I cant seem to find anything related to an "either or" scenario that would allow someone to access Events#show by either the event_id or the event_token. In my head, the following makes sense:
resources :events, params: :token || :id
Backstory:
I am setting up a "sharing link" so users without an account can view an events page, similar to google docs. To do this, a unique token is generated for each event, creating a websafe url (Protected sharing link in Ruby on Rails). The issue I am facing is trying to maintain routing using both the event_id and the event_token since the event page is also being viewed by account holders, where finding events by id is much easier.
Thank you.
Assuming that the url is the same shape -
# routes.rb
resources :events
# your urls
/events/15
/events/some-token-string
Rails will treat everything after the last slash as the id parameter. From there you can just query using an or clause
# Rails 5 with no raw SQL
Event.where(id: params[:id]).or(Event.where(token: params[:id])).first
# Rails 4 and lower (also works with Rails 5)
Event.where("id = ? or token = ?", params[:id], params[:id]).first
Can't think of any built-in mechanisms, but you could use friendly_id gem to achieve this.
Related
I added irwi ( https://github.com/alno/irwi ) to my app.
I'd like it accessible when user access to http://example.com/shop/:shop_name/wiki
(I need to know how routes.rb should be)
Anyone can show me how to make it?
:shop_name is slug so that it could be various pattern.
Of course each shop record should have a wikipedia ( one-to-one here. When shop is being created, one wikipedia page for it should be created automatically at the same time. )
I'm using Cancan, and devise for authentication so if possible, I want it only the registered user can edit and update wikipedia page.
Thanks.
see - http://guides.rubyonrails.org/routing.html for how to do your routing if you want more detailed help you will have to provide more code. Will be something along the lines of-
resource :wiki, :controller => :shop, :only => [] do
get :wiki
post :wiki
end
depending on what you require wiki to be able to do, your controllers etc. you should just be able to use the
before_filter :authenticate_user!
for your authentication
I looked around on how to change the dynamic params slot and found this post that does the exact thing.
The post is https://thoughtbot.com/blog/rails-patch-change-the-name-of-the-id-parameter-in
Basically what it does is, if following is the routes:
map.resources :clients, :key => :client_name do |client|
client.resources :sites, :key => :name do |site|
site.resources :articles, :key => :title
end
end
These routes create the following paths:
/clients/:client_name
/clients/:client_name/sites/:name
/clients/:client_name/sites/:site_name/articles/:title
One solution is to override the def to_param method in the model, but I want this without touching the model itself.
But since its for Rails 2.x, how can I achieve the same for Rails 3?
Update
This app is using Mongoid. Not AR.
So, the gem friendly cannot be used afaik.
Rails 4 & 5
In Rails 4, the :param option was added, which seems to do exactly what you're looking for. You can take a look at the Rails 3 code compared to the Rails 4 code.
Details
You can easily implement this in your routes.rb file:
# config/routes.rb
resources :posts, param: :slug
# app/controllers/posts_controller.rb
# ...
#post = Post.find_by(slug: params[:slug])
# ...
As of the release of Rails 4, this functionality is documented in the Rails Guides.
Rails 3
Unfortunately, in Rails 3, the :key option for resources was removed, so you can no longer easily change the name for routes created in this way by just passing in an extra option.
Details
I assume you've already somehow gotten the application working the way you want in the past year, but I will go into a way to get the effect you describe in Rails 3 in routes.rb. It will just involve a bit more work than the to_param method. You can still define custom parameters in routes defined using scope and match (or it's cousins get, put, post, and delete). You simply write in the parameter name you want in the matcher:
get 'clients/:client_name', :to => 'clients#show', :as => client
scope 'clients/:client_name' do
get 'sites/:name', :to => 'sites#show', :as => site
end
You would have to manually add all the routes that resources automatically creates for you, but it would achieve what you're looking for. You could also effectively use the :controller option with scope and additional scope blocks to take out some of the repetition.
EDIT (May 8, 2014): Make it more obvious the answer contains information for both Rails 3 & 4. Update the links to the code to go to exact line numbers and commits so that they should work for a longer period of time.
EDIT (Nov 16, 2014): Rails 4 should be at the top now and include relevant information as it's been the current version of Rails for quite some time now.
EDIT (Aug 9, 2016): Reflect that the solution still works in Rails 5, and update outdated links.
in Rails 4, pass param option to change the :id params. For example
resources :photos, param: :photo_name will generate /photos/:photo_name
In Rails 3 you can rename the id keys by using a combination of namespaces and scopes like this (not very nice though):
namespace :clients do
scope "/:client_name" do
namespace :sites do
scope "/:name" do
post "/:title" => "articles#create"
...
end
end
end
end
If I understand you correctly, what you want is to have the client_name instead of id in your url, right?
You can do that by overriding the to_param method in your model. You can get more information here.
There's a gem for that, just like there's a gem for everything ;)
I've been using FriendlyId for this kind of behaviour in rails 3.
It will require you to add some code to your model though, like this:
class Client < ActiveRecord::Base
has_friendly_id :name
end
...and if your clients don't have URI compatible names, you might want to use a slug for that, which you can do with has_friendly_id :name, :use_slug => true. When using slugs you'll obviously need to persist them to the database as well though.
And as already mentioned, you can still use the to_param trick with rails 3, as documented here. I find FriendlyId a bit more versatile though.
Working in Rails 3.2, I a polymorphic Subscription model whose subscribable_type may or may not be a nested resource. I'm trying to display the full URL link in an email view, but have no knowledge whether or not that resource is nested.
When I try url_for #model on a nested resource, it fails, expecting url_for [#parent, #model]. Unfortunately, I do not know how to discover the parent as defined in the Routes table.
Is there a way to identify the route path for a nested resource? If I could match the model to a route, I could fill in the necessary IDs.
As of right now, I've defined a method in my models called parent_resource :model that can be traversed, but I'm hoping there's a better way.
Within my routes.draw:
resources :projects do
resources :topics do
resources :comments
end
end
resources :subscriptions
(I realize I shouldn't be nesting so deeply)
Edit: Additional Information
My Subscription model is a resource I use to manage notifications. Subscribable types are provided a link that toggles the subscription for that user on that subscribable_type / subscribable_id on or off.
I then go through a Notifier < ActionMailer::Base which is provided the Subscription instance, and mail the user.
Through that setup, I'm trying to get the full url of subscription.subscribable which may be a Topic or a Project.
I realize that I could hammer out the conditions in this small case through a helper method, but I am curious to know how one would approach this if there were dozens of nested model pairs.
You mention subscription but your routes are completely different. I'm guessing the routes you gave were just an example then. I would start with trying to get rid of the custom parent_resource method you created. You can probably do the same thing simpler with adding a belongs_to through and maybe with conditions if you need too:
belongs_to :projects, :through => :topics, :conditions => ['whatever your conditions are']
I'd have one of those per parent type so I can do things like:
object.project.present?
And from there I could easily know if its nested or not and simplify things by letting rails do the parent traversal. That ought to simplify things enough to where you can at least figure out what type of subscription you have pretty easily. Next, I'd probably add some matched routes or try to cram an :as => 'somename' into my routes so I can call them directly after determining the nested part. One option would be something like this:
match "projects/subscription/:id" => "projects#subscription", :as => :project_subscription
match "other/subscription/:id" => "other#subscription", :as => :other_subscription
And so its pretty obvious to see how you can just specify which url you want now with something like:
if #object.project.present?
project_subscription_path(#object)
else
other_subscription_path(#object)
end
This may not be the best way to accomplish what I'm doing, but this works for me right now.
This builds a nested resource array off the shortest valid route helper and generates a URL:
(Tested in rails console)
resource = Comment.first
resource_name = resource.class.to_s.downcase
helper = Rails.application.routes.named_routes.helpers.grep(/.*#{resource_name}_path$/).first.to_s.split('_')
built = helper.slice!(-2,2) # Shortest possible valid helper, "comment_path"
while !(app.respond_to?(built.join("_").to_sym))
built.unshift helper.pop
end
built.pop # Get rid of "path"
resources = built.reverse.reduce([]) { |memo, name|
if name == resource_name
memo << resource
else
memo << memo.last.send(name.to_sym) # comment.topic, or topic.project (depends on belongs_to)
end
}
resources.reverse!
app.polymorphic_url(resources) # "http://www.example.com/projects/1/topics/1/comments/1"
I am a ruby/rails newbie and have a belongs_to relationship between, let's say, group and user (user belongs_to group).
I would like to have the following type of url:
www.mysite.com/abcd/user/1 - (NOTE "group" is not part of the URL)
where abcd is the group name and 1 is the user ID.
Is this easy to do? How do I go about it?
Thanks!
I'm not sure, but try this:
scope :path => '/:group_name' do
resources :users
end
You must find your group by params[:group_name] if you use this approach.
Yes, it is very easy to do in both Rails 2 and 3.
Assuming you're using Rails 3, you would do it like this
match ":group/user/:id", :to => "group#user"
When you use the match method, URL parts with a preceding colon will become parameters, so in your controller you would have params[:group] and params[:id] available. The :to paramater defines the controller and the action, so in this example it would send the request to the GroupController and the user action. :to can actually point to any Rack application end point, the "cont#action" is just a shorthand.
If you are using resources, you can set those up as normal, and then just define this non-standard route somewhere else in the stack and both will work side-by-side.
I'm using Ryan Bates' nifty authentication in my application for user signup and login. Each user has_many :widgets, but I'd like to allow users to browse other users' widgets. I'm thinking that a url scheme like /username/widgets/widget_id would make a lot of sense--it would keep all widget-related code in the same place (the widgets controller). However, I'm not sure how to use this style of URL in my app.
Right now my codebase is such that it permits logged-in users to browse only their own widgets, which live at /widgets/widget_id. What changes would I need to make to routes.rb, my models classes, and any place where links to a given widget are needed?
I've done Rails work before but am a newb when it comes to more complicated routing, etc, so I'd appreciate any feedback. Thanks for your consideration!
Look into nested routes. You could nest widgets inside users, like this:
map.resources :users do |users|
users.resources :widgets
end
Which would give you URLs like these:
/users/1/widgets # all of user 1's widgets
/users/1/widgets/1 # one of user 1's widgets
Check out the routing guide for more details.
The easiest would be to go with InheritedResources plugin which handles most of the legwork for you.
# routes:
map.resources :users do |user|
user.resources :widgets
end
class WidgetsController < InheritedResources::Base
# this will require :user_id to be passed on all requests
# #user will be set accordingly
# and widget will be searched in #user.widgets
belongs_to :user
end
# no changes required to the models