Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is it bad to leave unused REST actions?

I'm really confused at how to do this.

I'm tired and frustrated as well, and my brain is hurting from looking at so much different stuff.

  1. Is it poor practice to leave unused REST actions? I have a Resource model for example, and users can submit them, but I dont need the 'index' action to show them because that's handled by the Home#index controller. All i want to do is use link_to to go to the page with a form to add a new resource. So now all the link_to's im trying to do keep trying to go to /resources when i do resources_path. I read how you can use 'legacy' methods and just link right to the action in the controller but that is 'not the rails way'. now im trying to find out what is the rails way if im not using all the different REST actions it makes by default.

  2. does the above scenario sound like im doing this right? that i wanted it to be in home#index to list all the resources rather than resources#index ?

  3. If it IS poor practice, then how do i create them manually? all the tutorials i see online are "just type resource :user and you're good! it doesnt say what to do if you dont want/need all of them

  4. what if i want to change the url from the default? I dont particularly care for it but everyone in IRC is telling me this is 'wrong'. how is changing the URL wrong? I want it to be /resource/submit and not /resources/new. The singular/plural just seem to make more sense to me and looks more professional.

  5. Given that i leave all the url's the same. If i go to /resources/new and click 'submit' on the form it says

Template is missing

Missing template 
resources/create, application/create with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :coffee]}. Searched in: * "/Users/Zesty/Code/gem-portal/app/views" * "/usr/local/lib/ruby/gems/1.9.1/gems/devise-2.0.4/app/views"

if I have resources :resources in my routes i have no idea why its not automatically going to resources#create:

  def create
    @resource = Resource.new(params[:resource])
  end

Thanks!

Here's whats in my routes by the way, in case anything is glaringly wrong

  root  :to => 'home#index'

  resources :resources
  # match '/resource/submit',    :to => 'resources#new'

  match '/learn',    :to => 'pages#learn'
  match '/contact',  :to => 'pages#contact'
  match '/requests', :to => 'pages#requests'
like image 743
Tallboy Avatar asked Feb 27 '12 11:02

Tallboy


1 Answers

1. Is it poor practice to leave unused REST actions?
It is not poor practice as far as I can answer this. If you don't need an index action for a controller, you don't have it, period. But what you should is exclude this action in your routes, e.g. doing resource :resources, :except => [:index]. This can become handy when you are rescuing from errors because a NoTemplateError is not the same as a RoutingError (which means you could miss rescuing something in production).

Besides, it is a nice little helper for developing. When you accidentally write a link to the not-used controller index, you get an error on the page containing the link if you excluded the action in your routes. If you don't to this, you will only see the error after clicking the link because the route is valid (but has no function / template / whatsoever).

2. does the above scenario sound like im doing this right? that i wanted it to be in home#index to list all the resources rather than resources#index ?
I can't answer this without insight in your business logic / application workflow. But if you don't need it, you leave it.

If it is a cosmetic issue (e.g. resources#index is basically needed, but under another url), you could just create a route match "home", :to => "resources#index". This would deprecate your custom home controller and you could use your index action on resources which would be available under /home and under /resources. But again, this may be not the best option for your workflow/logic, so this is just a suggestion.

3. If it IS poor practice, then how do i create them manually?
There's an excellent page on the Rails guides page about all your routing needs. Have a look there.

4. what if i want to change the url from the default?
Rails way is convention over configuration. Means that those methods are rails convention. It is good to stick to this convention, so everytime you go back to your app changing things, you know at least how it works because of that convention. Even rails coders without any knowledge of your app can make modifications to it more quickly when everyone is sticking to this convention. This RoR vs PHP fun commercial explains this very good.

You can change those, but you shouldn't.

5. Given that i leave all the url's the same. If i go to /resources/new and click 'submit' on the form it says
After create, you usually redirect to something or render the new action within the create call to keep the information of your current object (e.g. errors) which would get lost when you'd do a redirect. You're currently staying on the create action (no render / redirect another action) so rails expects a create.html.erb template.

Example:

def create
  @resource = Resource.new(params[:resource])

  if @resource.save
    # record valid and saved, so redirecting to /resources/:id
    redirect_to @resource
  else
    # record not valid. rendering the new.html.erb template
    # (HINT the new action is NOT called here).
    render :action => :new
  end
end

Keep in mind that .new creates a new object, but it is not stored into the database unless you call e.g .save on it.


These are just my opinions and experiences on this, so if anyone more experienced thinks there is something wrong, I am glad to read a comment and update my answer.

like image 71
pdu Avatar answered Sep 18 '22 02:09

pdu