Using views as a backend for rails

31 views
Skip to first unread message

Ben Edwards

unread,
Sep 2, 2017, 10:26:42 PM9/2/17
to Ruby on Rails: Talk
Ive been reading quite a few guides on this and my first attempt was not successful.  What I have done so far is writern n a migration for the view and got that working:

class UpcommingEvents < ActiveRecord::Migration[5.1]
  def up
    self.connection.execute %Q( CREATE OR REPLACE VIEW upcoming_events AS
       SELECT   v.name         "Venue",
                to_char(e.start_date,'Day') "Day",
                e.start_time,
                e.title         "Event",
                e.description
        FROM    events e,
                venues v
        WHERE   v.id = e.venue_id
        AND     e.start_date
        BETWEEN (CURRENT_DATE -INTERVAL '1 day')::date
        AND     (CURRENT_DATE + INTERVAL '7 day')::date
        order by start_date, v.name
        )
  end

  def down
    execute "DROP VIEW upcoming_events"
  end
end

Created a controller:

class CurrentEventsController < ApplicationController
  def index
  end
end

And a Model

class CurrentEvent < ApplicationRecord
end

And a View

<p id="notice"><%= notice %></p>

<h1>Current_Events</h1>

<table>
  <thead>
    <tr>
      <th>Venue</th>
      <th>Day</th>
      <th>Start Time</th>
      <th>Event</th>
      <th>Description</th>
      <th colspan="3"></th>
    </tr>
  </thead>

  <tbody>
    <% current_event.each do |ce| %>
      <tr>
        <td><%= ce.venue %></td>
        <td><%= ce.day %></td>
        <td><%= ce.start_time %></td>
        <td><%= ce.event %></td>
        <td><%= ce.description %></td>
      </tr>
    <% end %>
  </tbody>
</table>

<br>

Bit when I add it as a route and browse  to the page I get

NameError in CurrentEvents#index


Showing /home/ben/eventpuddle/eventpuddle/rails/eventpuddle/app/views/current_events/index.html.erb where line #18 raised:

undefined local variable or method `current_event' for #<#<Class:0x007f64a41d4740>:0x007f648d40ba48>
Did you mean?  current_events_index_url

Extracted source (around line #18):
17 <tbody>
18 <% current_event.each do |ce| %>
19 <tr>
20 <td><%= ce.venue %></td>
21 <td><%= ce.day %></td>

Rails.root: /home/ben/eventpuddle/eventpuddle/rails/eventpuddle


I also added to config/application.rb


config.active_record.schema_format = :sql

and run 'rake db:migrate db:test:prepare'

Not sure where I am going wrong;(.









Ben Edwards

unread,
Sep 2, 2017, 10:33:36 PM9/2/17
to Ruby on Rails: Talk
Just noticed and error in the view, should of been

<% @current_events.each do |ce| %>

Now getting a different error:

undefined method `each' for nil:NilClass

Colin Law

unread,
Sep 3, 2017, 2:27:23 AM9/3/17
to Ruby on Rails: Talk
On 3 September 2017 at 03:33, Ben Edwards <lo...@funkytwig.com> wrote:
> Just noticed and error in the view, should of been
>
> <% @current_events.each do |ce| %>
>
> Now getting a different error:
>
> undefined method `each' for nil:NilClass

That is saying that you have tried to all each on an object that is
nil, so that means @current_events is nil, which is not surprising as
you have not set it anywhere. You need to setup @current_events to the
events you want to show in index method of the controller. So
something like
def index
@current_events = CurrentEvent.all
end

Colin

Christoph Lupprich

unread,
Sep 3, 2017, 2:40:58 AM9/3/17
to Ruby on Rails: Talk
In your CurrentEvents controller's index action you need to query for events, e.g.

class CurrentEventsController < ApplicationController
 
def index
   
@current_events = CurrentEvent.limit(100
 
end
end

Ben Edwards

unread,
Sep 5, 2017, 7:53:41 AM9/5/17
to Ruby on Rails: Talk
Thanks, that worked.  how come all the other controllers index action for everything else are empty? 

Colin Law

unread,
Sep 5, 2017, 8:38:18 AM9/5/17
to Ruby on Rails: Talk
On 5 September 2017 at 12:53, Ben Edwards <lo...@funkytwig.com> wrote:
> Thanks, that worked. how come all the other controllers index action for
> everything else are empty?

What is in the view for one of those index actions?

Colin
> --
> You received this message because you are subscribed to the Google Groups
> "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to rubyonrails-ta...@googlegroups.com.
> To post to this group, send email to rubyonra...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/rubyonrails-talk/ca34f18b-9004-4d0c-a905-8850d1aa49f2%40googlegroups.com.
>
> For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages