lili pupu lili pupu - 6 months ago 10
Ruby Question

I can't call my new variable, why not?

I'm new in rails, so I do not master even the subtleties of this langage.
I meet an error which say

undefined method 'prix' for Online::ActiveRecord_Associations
when I try to call my variable "prix" and "portion" into the post index.
So I can I do that ??

My code :

Onlines controller :



class OnlinesController < ApplicationController
before_action :authenticate_user!
before_action :set_post
before_action :owned_online, only: [:new, :edit, :update]
before_action :set_online

def index
@post = Post.all.order('created_at DESC')
end

def new
@online = current_user.onlines.build
@online.post_id = @post.id
@online.user_id = current_user.id
end

def create
@online = @post.onlines.create(online_params)
@online.user_id = current_user.id
@online.post_id = @post.id
if @online.save(online_params)
@online.update(push: true)
@online.update(pushed_at: Time.zone.now)
flash[:success] = 'Votre post est en ligne !'
redirect_to post_path(@post)
else
render 'new'
end
end

def show

end


def update
if @onlines.update(online_params)
if @online.push == false
if @online.portion <= 1
@online.update(push: false)
flash[:success] = 'Veuillez indiquer le nombre de parts disponibles '
redirect_to root_path
else
@online.update(push: true)

flash[:success] = 'Votre post a bien été pushé !'
redirect_to root_path
end
end
else
@user.errors.full_messages
flash[:error] = @user.errors.full_messages
render :edit
end
end


private

def online_params
params.require(:online).permit(:user_id, :post_id, :prix, :portion, :push, :pushed_at)
end

def owned_online
@post = Post.find(params[:post_id])
unless current_user == @post.user
flash[:alert] = "That post doesn't belong to you!"
redirect_to :back
end
end

def set_post
@post = Post.find_by(params[:post_id])
end


def set_online
@post = Post.find(params[:post_id])
@online = Online.find_by(params[:id])
end

end





Onlines/Index view :



<div class="title text-center">
<h1>Alors ? On mange quoi ?</h1>
</div>

<br>

<%= render 'posts/post' %>





posts /post view :



<div class="row">
<%-@posts.each do |post|%>

<div class="post">
<div class="form-group text-center">
<h3> <%=post.title%></h3>
</div>


<p> Posted by : <%= link_to post.user.pseudo, profile_path(post.user.pseudo) %>, <%= time_ago_in_words(post.created_at) %> ago </p>



**<p><%= post.onlines.prix %></p>
<p><%= @online.portion %></p>**

<div class="image text-center">
<div class="image-border">
<%= link_to (image_tag post.image.url(:medium), class:'img-responsive'), post_path(post)%>
</div>
</div>



</div>
<% end %>
</div>
</div>
</div>





So, if you have any ideas, tell me please !!

Answer

In your posts/post.html.erb

**<p><%= post.onlines.prix %></p>

You encountered the error undefined method 'prix' for Online::ActiveRecord_Associations because posts.onlines is a collection of one or more Online objects, and NOT a single Online record.

By this, I meant that the following values may differ (as an example):

post.onlines.first.prix
post.onlines.last.prix

If you want to select just the @online record for every specific post in your <%-@posts.each do |post|%> loop, then you could just do the following

posts/post.html.erb

...
**<p><%= @online.prix %></p>
<p><%= @online.portion %></p>**
...

Or if you want to show all prix values for each Online record in post.onlines, then you could do the following

posts/post.html.erb

...
<% post.onlines.each do |online| %>
  **<p><%= online.prix %></p>
<% end %>
<p><%= @online.portion %></p>
...
Comments