Use of "new do" in Rails 3 Routes - ruby-on-rails-3

I'm working with a Rails 3 Routes file and the resource mapping looks like this:
resources :projects do
new do
post :add_test_phase
post :add_client
post :refresh_form
end
I've read the Routes Guide for Rails 3 but find no mention of this. I know what "member" or "collection" add but am stumped by this new tag. Does it mean perform the mentioned posts when a new project is created?

It works just like the post do block does. It's just for creating a bunch of new routes. Your above example would give you add_test_phase_new_project_path mapped to projects#add_test_phase, add_client_new_project_path mapped to projects#add_client, refresh_form_new_project_path mapped to projects#refresh_form. The urls would be /projects/new/add_test_phase, /projects/new/add_client and /projects/new/refresh_form. Although, honestly, I don't really see a good use case for this.

Related

Rails ActsAsTaggable - Retrieve most_used tag of posts from user

With the ActsAsTaggableOn gem I would like to retrieve the most used tags used for posts by a certain user. Currently I have a user.rb model and a post.rb model which belongs_to the user. What I can do is this:
ActsAsTaggableOn::Tag.most_used
Which does show me the most used tags used overall. However, I would like to filter that down to only show me the most used tags by the current_user. I was thinking of something like:
ActsAsTaggableOn::Tag.most_used.joins(:posts).where(user_id: current_user.id)
which does not work since there is no connection established and therefore i cant join the models. How can I access the most used tags in the posts by the current_user?
ActsAsTaggableOn::Tag.joins(:taggable).where(taggable: current_user.posts).distinct.most_used(5)
After tinkering around with a lot of queries I got the solution now which might be a bit tricky.
Basically what ActsAsTaggableOn does is just assigning tags to the respective model. The tags however are in no way really related to anything else. What is possible is to assign tag ownership.
What I do now could be considered as bad practice: First the Post is assigned a tag which is stored in the tag_list, then I am going to trigger an after save action inside the posts.rb which grabs the last tag of the post object (only one tag can be assigned in my app) and assigns it to the user via
#user.tag(#post, with: "Some tag", on: :posts)
It would have been way easier to just code the whole thing myself and not rely on a gem, but its okay for now.

Rails 3: How to get a custom restful member route without ID

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.

Hide id from url in rails

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

How to create user specific content, that is invisible to others?

I am absolutely new to Ruby on Rails, even to programming at all. I got started with Michael Hartl's Rails Tutorial using Rails 3.0.10. Now I alter its aim towards creating an application that allows users to manage their own "projects". These projects are to be exclusively available to the logged-in user, thus, invisible to others.
My problem is: I am unable to create a page with an URL like "~/users/1/projects", I don't know about the routing. All i get done is "~/projects", which is fairly not what i want at all. So, how do I get this problem fixed? Or am I totally off track with that idea?
I generated a Projects model by scaffolding. So, how can I implement it for the signed-in users?
this would be done by creating a nested resource. when you are new to rails and programming you should work yourself a way through a lot of tutorials and guides.
a good place to get an overview are the official rails guides. in this specific case the chapter about routing: http://guides.rubyonrails.org/routing.html#nested-resources
# config/routes.rb
resources :users do
resources :projects
end

Using nested routes Rails 3 without actually allowing access to the parent resource

I'm using Rails 3 and I have two models EquipmentGroup and Reservation. I want reservations to be a nested resource of equipment groups so that I can access them with URLs like:
/equipment_groups/:equipment_group_id/reservations/:id
However, I don't want to create routes for the equipment groups. I can achieve this through the following, but it seems like a hack:
resources :equipment_groups, :only => [] do
resources :reservations
end
Is there a better way to do this? I can't seem to find an answer easily in the documentation.
Your approach - it's a standard approach, there is nothing better.
I can think of a few ways of doing this. One way is what you've done above. However, it seems like you have no need to expose the equipment groups controller or any of its actions, so the following should do just fine:
scope "/equipment_groups" do
resources :reservations
end
The scope block will append "/equipment_groups" to every route in it. This will essentially "fake" a nested route.