0

I am trying to get a Project form to build the first (starting) time of several (up to 12) volunteer time blocks.

project.rb

class Project < ActiveRecord::Base
  attr_accessible :title, ...
  has_many :vol_times, :dependent => destroy
  accepts_nested_attributes_for :vol_times, :reject_if => lambda { |a| a[:start_time].blank? }, :allow_destroy => true
  ...
end

vol_time.rb

class Vol_time < ActiveRecord::Base
  attr_accessible :start_time, ...
  belongs_to :project
end

ProjectsController

class ProjectsController < ApplicationController

  before_filter :signed_in_user, only: :create
  ...
  def new
    @project = Project.new
    @user = current_user
    @project.vol_times.build
  end
  ...
end

Vol_Times Controller

class Vol_TimesController < ApplicationController
  def new
    @reward = Reward.new
  end
  ...
end

My view looks like this...

<%= form_for(@project) do |f| %>

<div class="form_field_block">
      <p class="form_label">&nbsp;&nbsp;Project Title</p>
    <%= f.text_field :title, :size => 40, :placeholder => " Project Title...", :class => "frm" %>
</div>
<div class="form_field_block">
  <p class="form_label">&nbsp;&nbsp;Project Sub-title</p>
    <%= f.text_field :sub_title, :size => 40, :placeholder => "  Project Sub-title...", :class => "frm" %>
</div>
<p class="clearing"></p>
<div class="form_field_block">
  <% f.fields_for :vol_times do |builder| %>
    <%= render :partial => 'start_time', :f => builder %>
      <% end %>
</div>    
<p class="clearing"></p>
<%= button_tag "btn_start_project.png", :class => "btn_save" %>
<% end %>

And the _partial looks like this...

<%= f.label :start_time, "Starting Time" %>
<%= f.text_field :start_time %>

When I view the page, I see the containing <div>, but not the contents of the ERB, which should be parsed from the _partial.

Any ideas why this isn't working? I got the general context from Ryan Bates' RailsCast #196 - Here

1 Answer 1

1

you are missing a = on the fields_for. It should be

<%= f.fields_for :vol_times do |builder| %>
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, that fixed the generation issue, but now I'm getting NameError in Projects#new undefined local variable or method 'f' for #<#<Class called on the _partial on line #1: 1: <%= f.label :vol_times, "Starting Time" %> <%= f.text_field :vol_times, :size => 20, :placeholder => " Starting Time...", :class => "frm"%>
Ok, for those who may follow afterwards, the error in the comments above is due to the inclusion of :partial => in the render. It should read: <%= render 'start_time', :f => builder %>
the original syntax for rendering a partial is <%= render :partial => 'start_time', :locals => { :f => builder } %>. What you did is a shortcut and is preferred because it's cleaner. If you need to pass other options like collection and as, you still need to use the original syntax.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.