Routing to default resource but use the same resource with ID - ruby-on-rails

I'm using the default resource in my routes.rb to add clients to my applications via resources :clients. But I'd like to be able to access that new page of clients via a group_id aswell.
So I want access like /clients/new/ and as clients/new/1/ or something.
I've tried adding my group to my path like new_client_path(group) but it gives me a .3 and showing the params shows that the 3 is called 'format'.
So, Long story short: How can I get a group_id to my clients/new/ page?
Thanks in advance

You can use nested resources:
resources :groups
resources :clients
end
Now to get new client form with group id set you just need:
new_group_client_path(group)
assuming, of course, that group variable holds an Group instance.
If you don't want to use nested resources, you can set group_id this way
new_client_path(group_id: group)

Related

Rails: Configure routes to attempt to find records from two classes

This is my current route configuration:
resources :organizations, path: ''
resources :users, path: ''
I want to create a similar experience to what GitHub does. When using GitHub, you can access organization and user profile pages by entering "https://github.com/#{username}"
Now, the routes configuration above leads to the obvious problem that accessing organizations works fine while accessing a user fails because Rails only considers the organizations route and does not attempt to find a user.
Note: I am using friendly id to use usernames in my URL's and also made sure that usernames are unique across both ActiveRecord classes.
How do I do what I want to do?
You can create additional controller like PageOwnerController and pass request to it:
get ':page_owner_nick', to: 'page_owners#show', as: :page_owner
In show action you can manually find desired record by params[:page_owner_nick].
Advice: it looks like you have many a lot of similar logic between users and organizations - take a look on STI. Using STI allow you to write common code easier, but at the same time to separate different logic.

correctly configuring nested resources with shallowing

I've ran into my first issue with nested resources, and from the documentation
http://guides.rubyonrails.org/routing.html#limits-to-nesting
I'm not entirely able to figure out how to make sense of this, and how to apply it correctly to my situation, currently my things are setup like this:
resources :stores do
resources :locations do
resources :business_hours
end
end
now I'd like to limit the nesting, the way they recommend but I'm uncertain on how to achieve this, as locations belongs to stores, and business hours belongs to locations.
What the rails documentation is essentially saying is that with your resource configuration above you'll have a url that will look something like this on your web page.
mywebapplication.com/stores/1/locations/1/business_hours/1
with the corresponding rails helper method for your code
stores_locations_business_hours_url
Now there isn't really anything wrong with that and you can do it this way but you'll start to run into tedious problems especially with your business_hours controller. The reason being is because for your controllers you will have to pass in every #model object preceding the following. You'll have to do something like
stores_locations_business_hours_url([#store,#location,#business_hour])
to access a page. To limit that you will need to do something like this:
resources :stores do
resources :locations, shallow: true
end
resources :locations do
resources :business_hours
end
So now instead of mywebapplication.com/stores/1/locations/1 the url will like this mywebapplication.com/locations/1 and now your business hours url will be one level deep. That's what is meant by the documentation.
To go with what Rails wants you doing, all you have to do is add shallow: true to each of your nested resources:
resources :stores do
resources :locations, shallow: true do
resources :business_hours, shallow: true
end
end
This produces, as they put it, "routes with the minimal amount of information to uniquely identify the resource", which look like this:
Prefix Verb URI Pattern Controller#Action
location_business_hours GET /locations/:location_id/business_hours(.:format) business_hours#index
business_hour GET /business_hours/:id(.:format) business_hours#show
store_locations GET /stores/:store_id/locations(.:format) locations#index
location GET /locations/:id(.:format) locations#show
stores GET /stores(.:format) stores#index
store GET /stores/:id(.:format) stores#show
The collection actions for locations, eg. index, get nested under stores because locations belong to stores, but to identify a specific location, the route references locations/1 without the stores/ prefix, because you don't need a store ID to identify the location.
This cascades down the tree: to identify the business_hours collection actions, you need the location the hours belong to, but because you have a location ID, you don't need the store involved, so you get locations/:id/business_hours. When you want a specific set of hours, you don't need the location anymore, so you just get /business_hours/1.
If you want to maintain the entire hierarchy for the hours collection paths (that is, /stores/1/location/2/business_hours), you need to either not shallow your locations paths, which will keep their member actions (show, edit, etc.) under /stores/1/locations/2, or you'll need to manually specify the paths you want using less of Rails' helpers.

Rails - How to scope all routes in the app with user_ids

I'm looking to do what Basecamp does essentially, having my URIs be like this:
www.example.com/:user_id/
www.example.com/:user_id/projects/12314
Where you can only access that which is scoped under your user_id.
My ideas so far are:
To nest all other resources under users
To use a scope scope: ":user_id" do
And then just passing current_user whenever I create a path.
What's the best way to go about this?
Best approach is to implement nested routing(the one you are thinking of) to handle the above scenario, in which resources will be nested under parent. But you also need to think about the individual existence of resources whether they exists or not without user as sometimes few routes can exists without user so for that you also need to define them seperately in routes.rb.
as reference: http://guides.rubyonrails.org/routing.html#nested-resources
as example: http://blog.8thcolor.com/en/2011/08/nested-resources-with-independent-views-in-ruby-on-rails/

What are the advantages of using resource over resources in rails?

In rails routes what are the advantages of using
resources :user
resource :user
I have gone through Google and found that resource will not provide index method. Please post the differences if any.
From Rails guides:
Sometimes, you have a resource that clients always look up without referencing an ID. For example, you would like /profile to always show the profile of the currently logged in user. In this case, you can use a singular resource to map /profile (rather than /profile/:id)
This is why the resource will not provide index route, the purpose is map a single resource.
Reference
At a high level, the intent of resource is to declare that only one of these resources will ever exist. For example:
resource :profile, :only => [:edit, :update]
As a user, I should only be able to update my own profile. I should never be able to edit other users' profiles, so there's no need for a URL scheme like /users/1/profile/edit. Instead, I use /profile/edit, and the controller knows to use the current user's ID rather than the ID passed in the URL (since there is none).
That's why you don't get an index action with resource: there's only one resource, so there's no sense in "listing" them.

Rails 3 Attaching Model ID to every link

My application centers around an event and specifically the event's ID. Whenever a user navigates to different sections (controllers) of the site, I need to be able to carry the event's ID with it.
I'm guessing including the event's ID in the URL is the preferred method in case a user opens multiple browser windows.
I don't want to manually append the event's ID to every link. Is there a helper method I could create to do this for me?
Thanks
You need to create a nested resource in your routes file, this will add something like "/event/#eventid" to the beginning of your path. You can then access this from your controllers with params[:event_id]
eg:
routes.rb
resources :events do
# Other controllers routes go here
end
controller_whatever.rb
def index
#whatever = Event.find(params[:event_id]).whatever.all
end
...
Obviously it would be best to use a before filter, but you get the idea.
You should store that in session data:
session[:event_id] = event_id
You will then be able to access that throughout the user's session.
UPDATE:
You may want to have a look at nested resources.
I recommend to use thomasfedb's solution. If it isn't possible for any reason you could do it by overwriting the url_for method like in this question

Resources