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
home.html.erbgets rendered when you go tohttp://localhost:3000/lesson/homeprovided you have the appropriate route defined inroutes.rb:So in your
Lessoncontroller, in thehomeaction have the following:Now in your
home.html.erbview render the contents of@lessonsThe 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@lessonsvariable in the example above is accessible in thehome.html.erbview.I suggest having a read at the following for more info to do with routes and rendering views: