generate scaffold under devise user in rails 3.2 - ruby-on-rails

I use devise, and I want generate a new object scaffold under devise user sth like:
resources :users, :path => "/", :only => [:show] do
resources :collections, :controller => 'users/collections'
end
With above routes, I get this url:
http://localhost:3000/kevin_doe/collections
The problem is that if I issue this command:
rails g scaffold users/collection title:string description:text
This generate a namespace sth like:
namespace :users do resources :collections end
The route that I get is:
http://localhost:3000/users/collections
I want generate a scaffold under devise user resource.
How can I fix this problem?
Thank you!

The fix for this question are the next steps:
1º generate the scaffold with:
rails g scaffold collection title:string description:text
2º Modify your routes.rb file with the nested resource:
resources :users, :path => "/", :only => [:show] do
resources :collections, :controller => 'users/collections'
end
3º you must create a "users" folder in your controllers directory and move the collections_controller.rb to
app/controllers/users/
4º in collections_controller.rb you must modify:
class Users::CollectionsController < ApplicationController
.
.
.
end
5º In your views you must move the folder collections to app/views/users/collections
Done! :D.

Related

Rails routing issue: Unwanted route

I have a Ruby on Rails project. In my config/routes.rb, I have the following setup:
scope module: :comments do
resources :tasks do
resources :comments, only: %i[index]
end
end
This creates the route that I want in rake routes:
v1_task_comments GET /v1/tasks/:task_id/comments(.:format) v1/comments/comments#index
but it also creates the following route, which is wrong and which I don't want:
v1_tasks GET /v1/tasks(.:format) v1/comments/tasks#index
How do I create the first route without generating the second?
You can use the only option of resources routes helper:
resources :tasks, only: :none do
# ...
end

Two ways of registration rails

I have two type of users: master and customer (like freelance). I use devise gem.
How to create two ways of registration easier?
The best is to create a STI
class User < ActiveRecord::Base
end
class User::Master < User
end
class User::Customer < User
end
Install devise gem
rails g devise:install
Configure routes:
devise_for :users, skip: [:registrations]
devise_for :masters, skip: :sessions
devise_for :customers, skip: :sessions
Generate devise views
rails g devise:views
After you create helpers for the methods currency but adapting them to STI.

How can I alter url paths?

For example, because my controller is called listings, whenever I post a new listing it is for example domain.com/listings/listing-name however I would like it to be domain.com/businesses/listing-name instead.
How can I do this?
Current routes for this controller:
resources :listings do
member do
post :leadcreate
post :storycreate
end
end
Use path option
resources :listings, :path => "businesses" do
end
If you also want to rename the route helpers, then
resources :listings, :path => "businesses", :as => "businesses" do
end

How to rewrite URLs except for an action using namespaces in Ruby on Rails?

I am running Ruby on Rails 3 and I would like to set up my routes in order to rewrite URLs using namespaces, except for an action (the index action).
In the routes.rb file I have:
namespace "users", :path => "user" do
resources :accounts
end
So, for example, URLs to "show"/"create new" accounts are:
http://<site_name>/user/accounts/1
http://<site_name>/user/accounts/new
I would like to rewrite/redirect those URLs, except for the 'index' action, as/to
# For the 'index' action I would like to use plural 'users' instead of 'user'
http://<site_name>/users/accounts
# and
http://<site_name>/users
How to do that?
I tryed this
namespace "users", :path => "user", :except => :index do
resources :accounts
end
but it doesn't work.
try this
namespace "users", :path => "user" do
resources :accounts, :except => :index
end

Namespaced resources

This is an excerpt from my config/routes.rb file:
resources :accounts do |account|
account.resource :profile, :except => [:new, :create, :destroy]
account.resources :posts,
:collection => { :fragment => :get },
:has_many => [:comments, :likes]
# even more code
end
I would like that each nested resource to be loaded from from the account namespace such as Account::PostsController instead of PostsController.
Using resources :accounts, :namespace => 'account' tries to load AccountPostsController.
Trying to nest the structure doesn't really work all that well:
map.namespace :account do |account|
..
end
The previous code will load the files from the locations I want, however it does add the namespace to the url and the generated paths so I'll have methods such as account_account_posts_url and similar paths.
Another alternative is to use something like:
account.resource :profile, :controller => 'account/profile'
I really don't like this as it involves both code duplication and forces me to remove some of the rails magic helpers.
Any thoughts and suggestions?
Changing my routes.rb and running rake routes I came up with the following:
map.resources :accounts do |accounts|
accounts.namespace :account do |account|
account.resource :profile, :except => [:new, :create, :destroy]
end
end
This gets you what you want. The correct url and pointing to account/... controller.
See Rails Routing for more detailed info and options on what can be done with Rails Routes.
So what's specifically wrong with namespacing? I think this is what you're trying to do:
map.namespace :account do |account|
account.resource :profile
end
This will try to load the controller at app/controllers/account/profiles_controller.rb and will generate routes such as account_profile_path.
Updated based on comment:
map.resources :accounts do |account|
account.resource :profile
end
Will give you /accounts/22/profile.

Resources