Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Ruby

My own treebook - list by model attribute

I am creating my own app based on these tutorials.

Mine has a guideline and user model. A user can post a guideline with the attributes - title and content.

There may be many guidelines added by different users, with the same title but a different content e.g.

Guideline1: title is 'blue', content is 'apples' Guideline2: title is 'blue', content is 'bananas' Guideline3: title is 'red', content is 'grapes'

I would like to have a page where you could see a list of all the guidelines with the title 'blue'.

I have tried to do this in several ways but I can't seem to succeed. I have tried creating a news 'titles' controller and also just adding a 'titles' action to the 'guidelines' controller.

Can anyone advise me on how you would approach this so I know which line to follow and work on?

5 Answers

Christer Nordbø
Christer Nordbø
2,133 Points

Hi there, How are you storing these guidelines? Are you using a database connection?

If you are using MySQL you can do a select statement:

SELECT * FROM guidelines WHERE title = $title;

Hope this helps!

Aha - will try this! Thanks for the quick response.

Jason Seifer
STAFF
Jason Seifer
Treehouse Guest Teacher

The next stage in the Ruby Rapids, which will be out soon, also goes over something similar.

Ok good, because I still can't do it! Look forward to Ruby Rapids.

Here's where I'm up to on this. I have an action 'list' which lists all the guideline titles but removes duplictates i.e. would list 'blue', 'red', 'green' even if there are four guidelines with the title 'blue.

My code for this action is

def list

@guidelines = Guideline.order(:title)
@list=Array.new
@guidelines.each do |guideline|
  if !@list.include?(guideline.title)
    @list.push(guideline.title)
  end
  end

respond_to do |format|
  format.html # index.html.erb
  format.json { render json: @guidelines }
end

end

And in the list view contains:

<% @list.each do |title| %>

         <strong><%= link_to title, :action => :topic, :title => title %></strong>


    <% end %>

The list view and action seem to work correctly. You can click on the title and it will take you to a topic page that displays all the guidelines with that title.

The topic action is

def topic

@guidelines = Guideline.find_all_by_title(params[:title])



respond_to do |format|
  format.html # index.html.erb
  format.json { render json: @guidelines }
end

end

My topic view is:

<h1>All <%= @guidelines.title %> Guidelines</h1>

<%= link_to "Add a New Guideline", new_guideline_path, class: "btn btn-success" %>
<% @guidelines.each do |guideline| %>

<%= guideline.title %>

<a href="<%= guideline.content %>"><%= guideline.content %></a> 
<% end %>

Finally it works! But the content in the <h1> isn't working - I want it to display the topic title but the method is not recognised. Any advice?