Question

I am fairly new to Rails, and I am trying to use the jQuery newsticker from codecanyon to display the most recent event titles that have been entered into the database. Example here: http://codecanyon.net/item/jnewsticker-jquery-news-ticker/full_screen_preview/2137525

Right now, it is showing every entry in the database, and also all rows in the events table, instead of just the title, and I think the script is choking on that.

I want it to only show the 10 most recent events.

In my events_helper.rb helper I have:

module EventsHelper

    def populate_event
      @events.each do |event|
        content_tag(:li, link_to(event.title, '#'))
      end
    end

end

In my events_controller.rb controller I have:

class EventsController < ApplicationController
  before_filter :signed_in_user

  def create
    @event = current_user.events.build(params[:event])
    if @event.save
      flash[:success] = "Event created!"
      redirect_to root_url
    else
      render 'static_pages/home'
    end
  end

  def destroy
  end

  def show
    @event = Event.find(params[:id])
    @events = Event.recent
  end
end

In my event.rb model I have:

scope :recent, order(updated_at: 'DESC')

In my _ticker.html.erb partial I have

<ul id="newsticker_1" class="newsticker">
   <%= populate_event %>
</ul>

When I look at the source code in the browser, there are no <li> tags in the list.

It looks like this:

<ul id="newsticker_1" class="newsticker" style="position: absolute; left: 10px;">
   [#&lt;Event id: 29196, title: "This is a title", tag: nil, privacy_level: 1, group: nil, image_url: nil, start_date: nil, end_date: nil, start_location: nil, end_location: nil, start_geolocation: nil, end_geolocation: nil, content: "Quia officiis voluptatum doloribus cum ut ea sed ve...", user_id: 2, created_at: "2012-12-09 03:51:26", updated_at: "2012-12-09 03:51:26"&gt;, #&lt;Event id: 29190, title: "This is a title", tag: nil, privacy_level: 1, group: nil, image_url: nil, start_date: nil, end_date: nil, start_location: nil, end_location: nil, start_geolocation: nil, end_geolocation: nil, content: "Dolor consequatur sed enim omnis asperiores fugit r...", user_id: 2, created_at: "2012-12-09 03:51:26", updated_at: "2012-12-09 03:51:26"&gt;
</ul>

It should look something like this:

<ul id="newsticker_1" class="newsticker">
    <li>Lorem ipsum dolor sit amet, consectetur adipisicing elit...</li>
    <li>Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip...</li>
    <li>Duis aute irure dolor in reprehenderit in voluptate velit esse cillum...</li>
    <li>Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia...</li>
    <li>Bubble bubble ipsum dolor sit amet, consectetur adipisicing elit...</ li>
</ul>

UPDATE:

Following the suggestion of Dimuch below

This is what the ticker looks like in the browser: enter image description here

This is what the HTML source is doing:

enter image description here

Was it helpful?

Solution

You're misunderstanding helpers usage. It does not output content to generated page, it returns a value that is inserted to page.

So your helper method returns collection of events. Try to use map in combination with join instead.

def populate_event
  @events.map do |event|
    content_tag(:li, link_to(event.title, '#'))
  end.join
end

OTHER TIPS

After following dimuch's answer to fix the helper method, call the method with html_safe:

<ul id="newsticker_1" class="newsticker">
  <%= populate_event.html_safe %>
</ul>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top