Calling partials through helpers
Rails partials are are fragments of template code that can be inserted into a view. They can really simplify template code, by factoring out repeated code.
For example, I often put forms into a partial so that the form can be shared be create and edit views:
= error_messages_for :quiz
- form_for :quiz, @quiz, :url => quizzes_path, :html => {:method => :post} do |form|
= render :partial => 'quizzes/form', :locals => {:form => form}
= submit_tag 'Create'
= error_messages_for :quiz
- form_for :quiz, @quiz, :url => quiz_path(@quiz), :html => {:method => :put} do |form|
= render :partial => 'quizzes/form', :locals => {:form => form}
= submit_tag 'Update'
Many (most) people put the render call directly in their templates. This is not a good idea! What if you want to change the template path, or conditionally render a different template in certain conditions? Now you’ve got to repeat yourself in both templates, and that’s bad news.
A simple way to fix this is to always use a helper to render the partial. That way any changes can be made in one place, and it also a great place to store any messy conditional logic so that it doesn’t clutter up your templates.
= error_messages_for :quiz
- form_for :quiz, @quiz, :url => quizzes_path, :html => {:method => :post} do |form|
= quiz_form(form)
= submit_tag 'Create'
= error_messages_for :quiz
- form_for :quiz, @quiz, :url => quiz_path(@quiz), :html => {:method => :put} do |form|
= quiz_form(form)
= submit_tag 'Update
def quiz_form(form)
render :partial => 'quizzes/form', :locals => {:form => form}
end
Now if I want to do something, like say render a different form depending on a user’s preference, it’s easy to do.
= error_messages_for :quiz
- form_for :quiz, @quiz, :url => quizzes_path, :html => {:method => :post} do |form|
= quiz_form(form, current_user)
= submit_tag 'Create'
def quiz_form(form, user = nil)
if(!user.nil? && user.wants_advanced_quizzes))
render :partial => 'quizzes/advanced_form', :locals => {:form => form}
else
render :partial => 'quizzes/form', :locals => {:form => form}
end
end
Page Author
From Here You Can…
Information
- 233 Views
- 0 Comments
Most Recent Related Content
- Lesson
- Avatar

- Title
- World's Best Rails Hiring Process
- Body
- Here is my hiring process for Rails developers. This continues some thoughts ...
- Author
- Video
- Avatar

- Title
- Ruby Basics - Session 1
- Description
- Ruby Basics Screen CastSession 1
- Author
- Lesson
- Avatar

- Title
- Getting Started With Rails
- Body
- This guide covers getting up and running with Ruby on Rails. After reading it...
- Author
- Lesson
- Avatar

- Title
- Upgrading non-trivial apps to Rails 2.1
- Body
- For simple Ruby on Rails apps that use very few plugins, upgrading may be as ...
- Author
- Lesson
- Avatar

- Title
- How to use Fliqz4R
- Body
- About Fliqz Fliqz is the leader in full-service, plug-an...
- Author
- Lesson
- Avatar

- Title
- Action Controller: Streaming and file downloads
- Body
- Streaming and file downloads Sometimes you may want to send a file to the ...
- Author
- Lesson
- Avatar

- Title
- Action Controller: Session
- Body
- Session Your application has a session for each user in which you can stor...
- Author
- Lesson
- Avatar

- Title
- Ruby on Rails 2.1 What's new?
- Body
- Ruby on Rails What’s new? – English Version | View | Upload ...
- Author
