0

I'm using basic scaffold structure. What I need, is to add 'moderate' action and view by changing published to true. In my idea, on moderate.html I should get the list of all unpublished entries with the ability to change and save their parameters. Here are parts of my code:

 #names_controller.rb
 def moderate
 @name = Name.find(:all, :conditions => {:published => false} )
   respond_to do |format|
      format.html 
      format.xml  
     end
 end

#moderate.html.erb
<% form_for @name.each do |f| %>
  <%= f.error_messages %>
    <%= f.text_field :which %>  
    <%= f.text_field :what %>
    <%= f.check_box :published %>
    <%= f.submit %>
  </p>
<% end %>

Instead I'm getting this error:

NoMethodError in Names#moderate

Showing app/views/names/moderate.html.erb where line #1 raised:

undefined method `enumerable_enumerator_path' for #<ActionView::Base:0x1042c3e90>
Extracted source (around line #1)
 

So, can you help to newbie please?

ruby 1.8.7 (2009-06-12 patchlevel 174)

[universal-darwin10.0] Rails 2.3.5

Community
  • 1
  • 1
Maay
  • 575
  • 9
  • 27
  • Your problem is because you're passing @name.each as the variable in the form_for, but I am not sure of the solution. Are you trying to edit more than one record at once? – Ryan Bigg Oct 15 '10 at 01:50
  • Yes, I am. I need a list of all 'unpublished' records. The problem definitely is in combination of 'form_for' and '.each', but don't know any solution. – Maay Oct 15 '10 at 10:24

1 Answers1

1

If you want to update each name in a separate form, then all you need to do is move the loop above form_for:

<% @name.each do |n| %>
  <% form_for n do |f| %>
    <%= f.error_messages %>
    <%= f.text_field :which %>  
    <%= f.text_field :what %>
    <%= f.check_box :published %>
    <%= f.submit %>
   </p>
  <% end %>
<% end %>

But if you'd like to do it all in one submit (a single form) then I guess you can't use form_for. I'd use form_tag to create a custom form to update multiple instances. This should work both for create and edit form:

<%= form_tag moderate_names_path do %>  
  <% @names.each do |name| %>
    <fieldset>
    <%= fields_for "name[#{name.id}]", name do |name_fields| %>
      <p><%=name_fields.label(:this)%>: <br /><%= name_fields.text_field :this %></p>
      <p><%=name_fields.label(:that)%>: <br /><%= name_fields.text_field :that %></p>
      <p><%= name_fields.check_box :published %> <%=name_fields.label(:published)%></p>
    <% end %>
    </fieldset>
    <br />
  <% end %>  
  <%= submit_tag %>
<% end %>

NOTICE: I changed @name to @names in the second example

Matt
  • 5,328
  • 2
  • 30
  • 43
  • Thx for answer! First exemple works well, but it seems, that I need the second one and that's were I get error: 'SyntaxError in Names#moderate Showing app/views/names/moderate.html.erb where line #1 raised: compile error' – Maay Oct 15 '10 at 20:11
  • Ok, I've changed <%= %> to <% %> in 'form_tag' and 'fields_for' tags and now it's working. Thx again. – Maay Oct 15 '10 at 20:47
  • Yeah, sorry about that - I used my Rails 3 environment to code this example. – Matt Oct 15 '10 at 21:04
  • The last question: where should I define 'put' method to save the changes in the record? – Maay Oct 15 '10 at 21:13
  • You need to add the `moderate` route to your names model and then define a method with the same name in your `NamesController`. Or if you're asking where to set the form method, the `form_tag` helper accepts the `:method => :put` option. – Matt Oct 18 '10 at 10:40