views:

47

answers:

1

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

A: 

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
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#moderateShowing app/views/names/moderate.html.erb where line #1 raised:compile error'
Anton Axentyuk
Ok, I've changed <%= %> to <% %> in 'form_tag' and 'fields_for' tags and now it's working. Thx again.
Anton Axentyuk
Yeah, sorry about that - I used my Rails 3 environment to code this example.
Matt
The last question: where should I define 'put' method to save the changes in the record?
Anton Axentyuk
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