I'm building a website for a rabbit farmer (let's pretend). This man keeps a close eye on his rabbits, and wants them all categorized. So I built him a RabbitCategoriesController, and added this line to my routes.rb
resources :rabbit_categories
The URLs are showing up as rabbit_categories, rabbit_categoriew/new, etc.
What if I want the URLs to look like rabits/categories rabits/categories/new instead? This is not a nested resource, I just want to change the way the URLs look.
Of course, if the resources were called "categories", I could do
namespace :rabbits do
resources :categories
end
Is there any way I can write that, but tell it to use the RabbitCategoriesController instead of the Rabbits::CategoriesController?
have you tried this, should work
resources :rabbit_categories, :path => "rabbits/categories"
See Rails Routing from Outside In for more details.
Related
I'm working on a project where users can upload videos through a simple form and additionally by FTP to a certain directory and then simply choose the file from the FTP directory instead of uploading it through the form.
I got the following, pretty standard setup for a videos_controller:
# routes.rb
resources :videos
# new.html.rb
form_for(#video) do |f|
...
end
The restful actions in the controller are all working and just standard behaviour. The upload works, that's not the problem. The problem is if I do the following:
# routes.rb
resources :videos do
member do
post :from_ftp
end
end
# new.html.rb
form_for(#video, :url => from_ftp_video_url) do |f|
...
end
I get the error: No route matches {:action=>"from_ftp", :controller=>"videos"}, because the generated route looks like this:
from_ftp_video POST /videos/:id/from_ftp(.:format) videos#from_ftp
which seems right, since it's a member route. But I don't need the :id part of the URL, since I'm creating a new Video object, not through a form but simply by using the file from the FTP directory... So it basically is another create action, that's why I would like to do it as a POST request...
So how do I tackle this the best way?
Although the selected answer is correct for Vapire's situation, it doesn't necessarily answer the title question. If you came here looking for how to get member actions without an ID because you don't need an ID, the answer is a little different.
Say you implemented authentication that sets current_user. You let users edit their own profile only. In that case users/:id/edit doesn't make sense because :id is dictated by the current_user method. In this case /users/edit makes more sense.
You can change your routes.rb file to create member actions without an id in the path.
...instead of this...
resources :user
...use this (note the plurality of resource)...
resource :user
The way to understand member and collection routes is this:
Member routes do something to an object that you have.
Collection routes do something to the set of all objects.
So when we consider what the create route would be, it's a collection route, because it's adding a new object to the collection!
So your from_ftp method should also be a collection route, because it's adding to the collection.
Also, you might want to consider if you can accommodate the FTP functionality within your existing create method - it might be neater.
I have an existing website which I am attempting to port over to rails (3.2.7) and need to maintain the current urls.
Current website has urls like this:
http://example.com/Joe
http://example.com/Bob
Using rails the closest I have come is using the friendly_id gem and get this:
http://example.com/users/Joe
http://example.com/users/Bob
Every example I find seems to include the controller name in the url. How can I generate urls like the existing website?
Assuming you have :resources :users somewhere in your routes.rb you can put the next route definition in file:
match '/:name' => "users#show"
This way the url /Joe will direct to UsersController show action, populating params[:name] with the string 'Joe'.
You can find all the configuration steps needed here, 'Removing the controller names from URLs'
I am new to rails. I have developed an application on rails recently. The application is pretty big and it's running fine. Currently i have url like this.
http://192.168.99.220/user/13/domainUsers
I want it to be like the below one (without any id)
http://192.168.99.220/user/domainUsers
My routes are like this.
match 'user/:id/domainUsers', :to => 'domains#manageDomain_2', :as => :manageDomain2
I have tried to rewrite the url using "to_param". As my application is too big and it has lots of functionalities, i am using parameters other than the "id" to find users informations frequently, so i am not being able to use the "to_param" method. Is there any other way to hide "id" from url.
Please help
Thanks in advance.
The easiest way to do this is with a gem called friendly_id.
Here is a tutorial that explains it quite well:
http://railscasts.com/episodes/314-pretty-urls-with-friendlyid
So I just started using Dynamic segments as I need them to specify certain elements for grabbing data from AWS S3 via HTTParty.
I have this match statement in my routes.rb file:
match ':installation/:venue/:controller(/:action(/:id))'
This works great and allows me to do exactly what I want to do, which is pull in the installation and venue and use them with HTTParty to get their corresponding information from S3.
Now I need to keep my links through out persistent like these due to the fact that my application controller reads these in. So for example when I write a link_to I have had to do the following in a view/partial:
<%= link_to some_name,
"#{#installation}/#{#venue}/#{controller.controller_name}/show/some_id" %>
If it was just this ugliness I had to deal with that wouldn't be a problem, but I don't understand how I can pass around options in regards to this.
So basically is there a way to have resourceful routes for dynamic segments?
You can use polimorphic_url
http://apidock.com/rails/ActionDispatch/Routing/PolymorphicRoutes/polymorphic_url
I am writing a rails application and getting stuck on a routing issue.
My application allows a user to manage multiple cause pages (for various reasons)
right now, I am trying to build the admin screens for users to update their site.
for example, cause pages could look like:
.com/causes/1
.com/causes/2
I and want the admin URL to be:
.com/causes/1/admin/updates
.com/causes/2/admin/updates
etc.
How would I setup my routes to do this
I originally thought something like:
namespace "admin" do
resources :updates
end
But how can I prefix that with the cause/:id so that I can relate which cause I am updating?
You will need to have an admin_controller of sorts and you can setup your routes like so:
resources :admin do
#insert any resource you need to have admin pages on here
resources :causes do
resources :updates do
end
end
end
something like that. Or you could try some rails admin gems to make your life easier like: http://www.activescaffold.com/