Adding Rails Individual Route - ruby-on-rails

I have a CRUD resource defined in my routes.rb file: resource :user.
I'm adding a new controller method for the user called search_places, which is performed on the user to find other users with the same places. I'm adding a route it.
Right now, I have:
post '/user/search_place', which isn't very DRY. I'm new to Rails and I was reading the Rails routing documentation and figured that I could possibly use
resource :user do
 collection do
   post 'search_place'
 end
end
Is this considered good practice? I know this works (it passes my rspec route test), but is that how its best done?
Thank you,

When you add second don't need of first.
Add this:
resources :user do
collection do
post 'search_place'
end
end
Remove this:
resources :user
That makes DRY :)
Suggestion: Resources name should be defined in plural if u follow rails convention. (i.e) resources :users

Related

How the rails find the controller from routes?

I am learning rails. I would like to check the convention here,
say, if I have a comment resource, will the rails always to find action in the comments_controller, no matter the rource is independent or nested in another resource?
A:
resources :comments
B:
resources :blogs do
resources :comments
end
Yes, it will always use CommentsController. But using nested resources you will also have the blog_id set in params. You can read more on here, with detailed description and examples: http://guides.rubyonrails.org/routing.html#nested-resources

ROR: Routes issue - Survey gem

The Survey gem is not creating the routes on my Rails app so I am wondering what to put in the routes.rb file?
I ran
rails generate survey:install
then ran
rails generate survey routes namespace:survey
and does not work.
I'm using Rails 4.2.1
The controllers are at controllers/survey/attempts_controller.rb and controllers/survey/surveys_controller.rb
The views are at survey/attempts/ and survey/surveys/
How should I put in the routes for these? Thanks.
Survey uses a standard CRUD interface, so you should be able to add resources named after the survey model you've created.
i.e. resources :surveys
If this doesn't do the trick, you can see a basic routing setup in the survey demo app here: https://github.com/runtimerevolution/survey-demo/blob/master/config/routes.rb
Let me know if this helps!
Its okay if it didn't add routes to your routes.rb file. You can add it yourself. And since you have it namespaced within Survey for your controllers you have to namespace in your routes file too.
namespace :survey do
resources :surveys
resources :attempts
get 'survey_details' => 'surveys#get_details' #this request will be handled by get_details method of SurveysController
end
Since it uses basic Create Read Update Delete in its controller, resources :surveys will take care of these. For any additional routes you want to define for your controllers make sure to put them inside the namespace block.

Change Rails namespaced route to personalized params route

TL;DR: I want to have username621/posts/title-of-post instead of member/posts/1
The changing of post id to post title was easy enough since I used the freindly_id gem to generate the slugs.
However, I am having difficulty routing to a personalized params route instead of the current namespaced route. Here is the current routing:
namespace :member do
resources :posts
end
I want to replace the member namespace to user's username. So if their username is user123, the route should be user123/posts/title-of-post.
I think that this is not very standard Rails routing and tried looking for similar questions with no results.
for more complicated routes.rb, add a path option
namespace :member, path: ":user_id" do
resources :posts
end
should get what you want, e.g. http://localhost:3000/621/posts/1
then we just have to add friendly_id to User and Post to have it become something like http://localhost:3000/username621/posts/title-of-post
however, you'll need to pore through the codebase for things like member_post_path(post) and change to member_post_path(post.user, post)
Try removing the namespace and adding path option:
resources :posts, path: '/:username/posts/'
Then if you access /username621/posts/title-of-post in your controller you'll see params[:username] = 'username621'
If you have other paths of the form /something/posts add them above this route, otherwise they will be caught by :username.

RESTful nested controller in has_one relationship

Say I have a User that has_one ContactInfo.
An unrestful way to edit the contact_info would be to do this all through a single controller with a route of:
myapp.com/users/15/edit_contact_info
A more restful way would be to use two controllers, and route it like this:
myapp.com/users/15/contact_infos/23/edit
However, I don't like this, as the route includes the contact_info_id, which isn't really necessary for identifying the correct contact_info to update. Additionally, the contact_info_id is a confusing number for a user to see. (They may know their own user id, but the contact_info_id will seem like an arbitrary number).
Is there any way to RESTfully route like below:
myapp.com/users/15/contact_infos/edit
or something similar? Is this a bad idea?
resources :users do
get "contact_info/edit" => 'users#edit_contact_info'
end
I'd used a plural route, instead of a singular route. With the singular route, I get myapp.com/users/15/contact_info/edit.
Had:
resources :users do
resources :contact_infos
end
Changed to
resources :users do
resource :contact_info
end

What's the proper way to setup routes to get a model through another model?

I have 2 models that I would like to setup routes for: Apps and Issues
Apps have many issues and each issue belongs to an App.
I want the URL to represent the fact that issues never come without its app. So something like this:
website.com/app-name/issues/2
But i'm not sure how to set that up properly in the routes. I also validate the uniqueness of app names, so I won't have any overlap issue.
You'd like to use rails nested resources: http://guides.rubyonrails.org/routing.html#nested-resources
Basically:
resources :app do
resources :issues
end
More info on customizing your route to cater to the app name here:
Routing nested resources in Rails 3
try this
scope :app_name, :as => '' do
resources :issues
end
then rake routes to see what methods generated

Resources