Jim's
Tutorials

Spring 2019
course
site

Markdown interpretter

I added the redcarpet ruby gem to my file to interpret and display markdown syntax properly. Getting it to run was as easy as adding gem redcarpet to my gemfile and then typing bundle install into my terminal. To use it effectively I had to create a helper method in /app/helpers/application_helper. I wrote a function that takes some text as an argument and returns an html rendered version of that text.

module ApplicationHelper
  def markdown(text)
    markdown = Redcarpet::Markdown.new(Redcarpet::Render::HTML,
                                         no_intra_emphasis: true,
                                         fenced_code_blocks: true,
                                         disable_indented_code_blocks: true,
                                         autolink: true,
                                         tables: true,
                                         underline: true,
                                         highlight: true
                                        )
    return markdown.render(text).html_safe
  end
end

I then called this method in two different views to display text saved in the database as properly rendered markdown, here's an example from the studentworks show view:

<h1><%=@studentwork.user.username + "'s work"%></h1>
<p><%=markdown(@studentwork.work)%></p>
<p><%=link_to "Edit", edit_assignment_studentwork_path(@assignment.id, @studentwork.id)%></p>

I also refactored the assignments controller to conditionally send data based on the user who is logged in:

  def show
    @assignment = Assignment.find(params[:id])
    @user = current_user
    if Studentwork.all != []
      if @user.role == "admin"
        @studentworks = Studentwork.where(assignment_id: @assignment.id)
      end
      if @user.role == "student"
        @work = Studentwork.where(user_id: @user.id).take
      end
    end
  end

Now there are checks in the view and the server for who is logged in.