1

I've got a pretty simple Ruby on Rails app that has a text box where I can enter content, limited to 200 characters, into a database.

My goal is to list the items below the text box so that when a user enters something, it appears at the top of an ever-growing list.

Right now I have some crappy code that doesn't work, but I'm not sure why.

In my view page, located at home.html.erb, I have this area for the lessons to be viewed in:

<td class="my-lessons">

</td>

In my Lesson controller I have the following:

  def printLesson
    @lesson = Lesson.find(params[:id])

    puts @lesson.content
  end

I'm not sure how to call printLesson in my code and just generally how to show the content of the Lessons database.

Any help would be greatly appreciated

2
  • It seems you are not using RESTful routing, am I right? Commented May 14, 2012 at 6:50
  • I believe I am. I'm not advanced enough to do anything else right now. I'm not calling the method properly, I know that. I'm not sure how to call that method and have it spit out the contents of the first Lesson Commented May 14, 2012 at 6:53

1 Answer 1

3

home.html.erb gets rendered when you go to http://localhost:3000/lesson/home provided you have the appropriate route defined in routes.rb:

match 'lesson/home' => 'lesson#home'

So in your Lesson controller, in the home action have the following:

def home
  @lessons = Lesson.all
end

Now in your home.html.erb view render the contents of @lessons

<% @lessons.each do |lesson| %>
<tr>
    <td class="my-lessons">
      <%= lesson.Name %>
    </td>
</tr>
<% end %>

The problem you're having is thinking that you have to tell Rails to spit out the input in your controller action (the line puts @lesson.content). Rails works on convention. Any instance variables created in your controller's action will be accessible in your view. So the @lessons variable in the example above is accessible in the home.html.erb view.

I suggest having a read at the following for more info to do with routes and rendering views:

Sign up to request clarification or add additional context in comments.

4 Comments

One miscommunication I think that came from my end, the route to home.html.erb is app/views/pages/home.html.erb rather than app/views/lessons/home.html.erb. Any idea what kind of routing finagling needs to be done?
The location of the views in the app don't correspond to paths in the URL. You're far better of following Rails conventions and putting it in app/views/lesson/home.html.erb.
Thanks Soliah. So I've got that working, showing those lessons. What's the most efficient way to get those same things showing up in views/pages/home.html.erb?
The easiest for you would be to move the code from the lesson_controller.rb to a new controller page_controller.rb, renaming instances of Lesson to Page. Then modify the route to go to pages instead.

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.