M
M
Max2016-03-16 22:17:36
Ruby on Rails
Max, 2016-03-16 22:17:36

How to correctly access data (Rails)?

Hello, I am making a website that will allow you to book a place in a cinema.
There are sessions and there are seats.
Links:
Place
belongs_to :session
Session
has_many :places
When I reserve a place, the ID of the cinema, the session and the number of the reserved seat itself are stored in the database.
Session controller code

def session_places
@session = Session.find(params[:id])
end

Code for displaying all seats in the cinema, with a color change for an already booked seat
<h1>Show all session places</h1>
<% n = 0 %>
<% @session.number_of_session_places.times do |session| %>
<% n = n + 1 %>
<div class="place_block" style="<% if @session.places.place == n %>background-color: black <% end %>">
<%= n %>
</div>
<% end %>

Error at
if @session.place.place
undefined method `place'.
this should be the number of the booked place in the table (Places). Explain how I can get to it please.
It is worth noting that in the console I was able to get to the seat number like this:
ssession.places.last.place

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Roman Mirilaczvili, 2016-03-16
@maxprof

Link specifiedplaces
undefined method `place'.
and you use place.

It is worth noting that in the console I was able to get to the seat number like this:
ssession.places.last.place
This is some nonsense.
session.places.lastand there is an instance of the Place class. Do you get place.place ?!

V
Vasily Shakhunov, 2016-03-17
@inf

Sessions contain many Locations. Therefore, Session Locations still need to be looped through. Something like:

<% @session.number_of_session_places.times do |session| %>
    <% session.places.each do |p| %>
        Я место этого сеанса <%= p.place %>
    <% end %>
<% end %>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question