ROR: Routes issue - Survey gem - ruby-on-rails

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.

Related

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.

How to structure nested resources - Rails

In my Rails 4 app, an admin can moderate/approve posts, user accounts, and photos. I already have controllers for each of these (PostsController, UsersController, PhotosController) that handle the basic CRUD operations initiated by the user.
Having the update method for each controller seems incorrect and ... dirty. So does creating a single ModerationController with non-RESTful methods for each of the models.
I think I need something like a ModeratePostsController for each of the models, but I'm not sure how that gets scoped (under /admin?) or nested in routes.rb and generated as a controller.
Thoughts?
I was pretty much there.
I ran rails g controller Admin::Posts to create /app/controllers/admin/posts_controller.rb and in the routes file I added:
namespace :admin do
resources :teams
end

Adding Rails Individual Route

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

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

Retrieve the controller name from a class name following RoR conventions

I am using Ruby on Rails 3.0.9 and I would like to build a controller name from a class name as well as possible following RoR naming conventions. For example, if I have the Articles::Comment class I would like to retrieve the articles/comments string.
Maybe it exists a RoR method created by developers to handle internally these conventions, but I don't know that.
How can I retrieve the controller name as in the example above?
You're looking for the underscore method. More info here.
"Articles::Comment".underscore
Or, if you've got the controller class itself, it would be like this:
Articles::Comment.name.underscore
EDIT
As of routes, they are built one piece at a time, even when you namespace them. When you do something like this:
map.resources :articles do |articles|
articles.resources :comments
end
What rails is going to do is, first:
"articles". classify # which yields "Article" then rails is going to append "Controller" to it
Then it's going to get "comments" and do the same, but this one is going to be routed under "/articles". Rails does not namespace internal resources, so, the controller has to be CommentsController, not Articles::CommentsController.
Only then you clearly namespace something, Rails is going to namespace your classes:
map.namespace :admin do |admin|
admin.resources :articles # will require controller "Admin::ArticlesController"
end
I hope it's clearer now.

Resources