resource vs. resources - ruby-on-rails-3

I have a User model which has_one :blog. In my routes.rb file I have resource :blog. Why doesn't it create a path for blog#show? Do I need to create one myself? Otherwise how do I link_to blog#show?

It does create a path for blog#show - see the output of your routes by running rake routes. The route missing is blog#index, because there is only one to show.
See http://guides.rubyonrails.org/routing.html#singular-resources for more information on singular routes.

Related

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.

Rails 3 seo friendly urls without controller name

I have an existing website which I am attempting to port over to rails (3.2.7) and need to maintain the current urls.
Current website has urls like this:
http://example.com/Joe
http://example.com/Bob
Using rails the closest I have come is using the friendly_id gem and get this:
http://example.com/users/Joe
http://example.com/users/Bob
Every example I find seems to include the controller name in the url. How can I generate urls like the existing website?
Assuming you have :resources :users somewhere in your routes.rb you can put the next route definition in file:
match '/:name' => "users#show"
This way the url /Joe will direct to UsersController show action, populating params[:name] with the string 'Joe'.
You can find all the configuration steps needed here, 'Removing the controller names from URLs'

The relationship between routes.rb and the "paths" that link to pages?

This question stems from Hartl's Rails Tutorial (progressed in chapter 9) - sorry if it seems particularly noobish...
Currently, I understand that in the routes.rb file, when a page was defined using, for example:
match '/help', to: 'static_pages#help'
a link generated in an embedded ruby:
<li><%= link_to "Help", help_path %></li>
would function. The help_path, and specifically the word "path", would correspond to the "#help" defined in routes.
However, the routes file did not define links for items such as users_path (used to show all users), or edit_user_path(current user) (used to edit settings of the current user) - I was confused as to where they were defined, and how they are able to correctly function and link me the desired places.
Thanks!
The routes file does, in fact, get an entry determining the behavior of users (shown in listing 2.2). It gets created through the scaffolding described here.
resources :users
By convention a "resource" entry in your routes.rb file sets up a number of paths that will be associated with users. Refer to the Rails Guide for a good overview.

change the URL without changing the resource name

I'm building a website for a rabbit farmer (let's pretend). This man keeps a close eye on his rabbits, and wants them all categorized. So I built him a RabbitCategoriesController, and added this line to my routes.rb
resources :rabbit_categories
The URLs are showing up as rabbit_categories, rabbit_categoriew/new, etc.
What if I want the URLs to look like rabits/categories rabits/categories/new instead? This is not a nested resource, I just want to change the way the URLs look.
Of course, if the resources were called "categories", I could do
namespace :rabbits do
resources :categories
end
Is there any way I can write that, but tell it to use the RabbitCategoriesController instead of the Rabbits::CategoriesController?
have you tried this, should work
resources :rabbit_categories, :path => "rabbits/categories"
See Rails Routing from Outside In for more details.

How to determine link_to path arguments for nested resources?

How do I determine the link_to arguments for show, edit, and destroy? I know "new" is "new_user_post_path". Nothing else seems to follow this convention though?
My relevant routes.rb snippet:
resources :users do
resources :posts
end
Running
rake routes
from your rails root should tell you what you need to know in terms of the name of the routes and the parameters expected.