Moshe
Moshe

Reputation: 493

displaying records in ruby on rails using a conditional filter breaking down

How can I display all the records in a table if there is no params[:subject_id] sent, or filter the records that it will display if the params[:subject_id] is sent?.

So far I have a before filter in the pages controller to check if the params[:subject_id] were sent and creates an instance variable.

  private

  def find_subject
    if params[:subject_id]
      @subject = Subject.find_by_id(params[:subject_id])
    else
      @subject = Subject.all
    end
  end

problem is in the action when done like this @pages only stores the last record but won't break anything in the view.

  before_filter :find_subject

  def list
    @subject.each do |subject|
      @pages = Page.order("pages.position ASC").where(:subject_id => subject.id)
    end
  end

however if i do it like this

  def list
      @pages = Page.order("pages.position ASC").where(:subject_id => @subject.id)
  end

no records are returned the returned at all, when i check the output in the webrick console I see this

Subject Load (0.2ms)  SELECT `subjects`.* FROM `subjects` 
Page Load (0.4ms)  SELECT `pages`.* FROM `pages` WHERE `pages`.`subject_id` = 2186859380 ORDER BY pages.position ASC

Im not sure how its getting that id of 2186859380

I have also tired it this way

  def list
    if @subject.object_id.nil?
      @pages = Page.order("pages.position ASC")
    else
      @pages = Page.order("pages.position ASC").where(:subject_id => @subject.object_id)
    end
  end

however this also throws some errors and breaks my view as well.

here is my view so you can get a complete picture

<div class="page list">
    <h2>Pages</h2>
<%= link_to "Add a new Page", {:action => "new", :subject_id => @subject.id}, :class => "action new" %>
    <table class="listing" summary="Pages List">
        <tr class="header">
            <th>&nbsp;</th>
            <th>Subject</th>
            <th>Page</th>
            <th>Visible</th>
            <th>Sections</th>
            <th>Permalink</th>
            <th>Actions</th>
        </tr>
        <% @pages.each do |page| %>
        <tr class="<%= cycle("odd", "even") %>">
            <td><%= page.position %></td>
            <td><%= page.subject.name %></td>
            <td><%= page.name %></td>
            <td class="center"><%= page.visible ? "Yes" : "No" %></td>
            <td class="center"><%= page.sections.size %></td>
            <td><%= page.permalink %></td>
            <td class="actions">
                <%= link_to("Sections", {:controller => "sections", :action => "list", :page_id => page.id}, :class => "action delete")%>
                <%= link_to("Show", {:action => "show", :id => page.id, :subject_id => @subject.id}, :class => "action show")%>
                <%= link_to("Edit", {:action => "edit", :id => page.id, :subject_id => @subject.id}, :class => "action edit")%>
                <%= link_to("Delete", {:action => "delete", :id => page.id, :subject_id => @subject.id}, :class => "action delete")%>
            </td>
        </tr>
        <% end %>
    </table>
</div>

Any help on what I am doing wrong would be greatly appreciated.

Upvotes: 0

Views: 293

Answers (1)

Dougui
Dougui

Reputation: 7230

I think you should add a "s" to subject like here :

  private

  def find_subject
    if params[:subject_id]
      @subject = Subject.find_by_id(params[:subject_id])
    else
      @subjects = Subject.all
    end
  end

So now, you can do this :

def list
  if subjects
    @subject.each do |subject|
      @pages = Page.order("pages.position ASC").where(:subject_id => subject.id)
    end
  else
    @pages = Page.order("pages.position ASC").where(:subject_id => @subject.id)
  end
end

You could do something similar in your view...

Upvotes: 1

Related Questions