Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 679709
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T01:17:25+00:00 2026-05-14T01:17:25+00:00

Ryan Bates’ nifty_scaffolding, for example, does this edit.html.erb <%= render :partial => ‘form’ %>

  • 0

Ryan Bates’ nifty_scaffolding, for example, does this

edit.html.erb

<%= render :partial => 'form' %>

new.html.erb

<%= render :partial => 'form' %>

_form.html.erb

<%= form_for @some_object_defined_in_action %>

That hidden state makes me feel uncomfortable, so I usually like to do this

edit.html.erb

<%= render :partial => 'form', :locals => { :object => @my_object } %>

_form.html.erb

<%= form_for object %>

So which is better: a) having partials access instance variables or b) passing a partial all the variables it needs?

I’ve been opting for b) as of late, but I did run into a little pickle:

some_action.html.erb

<% @dad.sons.each do |a_son| %>
<%= render :partial => 'partial', :locals => { :son => a_son } %>
<% end %>

_partial.html.erb

The son's name is <%= son.name %>
The dad's name is <%= son.dad.name %>

son.dad makes a database call to fetch the dad! So I would either have to access @dad, which would be going back to a) having partials access instance variables or I would have to pass @dad in locals, changing render :partial to
<%= render :partial => ‘partial’, :locals => { :dad => @dad, :son => a_son } %>, and for some reason passing a bunch of vars to my partial makes me feel uncomfortable. Maybe others feel this way as well.

Hopefully that made some sense. Looking for some insight into this whole thing… Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T01:17:25+00:00Added an answer on May 14, 2026 at 1:17 am

    In recent versions of Rails it is quite a bit easier to render partials and pass locals to them. Instead of this.

    <%= render :partial => 'form', :locals => { :item => @item } %>
    

    You can do this.

    <%= render 'form', :item => @item %>
    

    I don’t do this in the Nifty Scaffold generator to keep backwards compatibility, but I’ll change this in a future release.

    As for whether it’s acceptable to use instance variables in partials. I think it is. In all practicality, what is the downside? Certainly things can get out of hand if you aren’t consistent, but I like to apply these guidelines.

    1. Never create an instance variable just to share it between partials. Usually this means you will only be sharing the controller resource object.

    2. If the partial is the same name as the resource, pass it as a local with <%= render @item %>.

    3. If the partial will be shared across multiple controllers then only use locals.

    This is what works well for me anyway.

    Bonus tip: if you find yourself passing in a lot of locals into a partial and you want some of them to be optional, create a helper method which renders the partial. Then always go through the helper method so you can make a clean interface with optional args for rendering the partial.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I found this in Ryan Bates' railscast site, but not sure how it works.
I'm trying to build [Ryan Bates' authenticaion from scratch][1] using sinatra but authentication does
I have a complicated nested form (Ryan Bates' version) with .live() attached to some
Taking Ryan Bates' asciicast as an example: http://asciicasts.com/episodes/163-self-referential-association He ends with two associations of
I have created a new project using devise login according to Ryan Bates' railscast
I'm creating a multi-part form in the style that Ryan Bates describes here: http://railscasts.com/episodes/217-multistep-forms
Im thinking about implementing this multistep wizard discussed in ryan bates railscast. http://railscasts.com/episodes/346-wizard-forms-with-wicked Im
I am creating a nested form and using Ryan Bates Railscast. I am creating
I'm using jquery_token_input after following Ryan Bates railscast #258 and this is the code
I'm still trying to get my form working using Ryan Bates Nested_fom plugin. The

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.