2016-05-10 4 views
-1

私はレールが新しく、このラングゲージの微妙ささえも習得しません。 変数「prix」と「部分」をポストインデックスに呼び出そうとすると、undefined method 'prix' for Online::ActiveRecord_Associationsというエラーが発生しました。 私はそれをすることができますか?私は新しい変数を呼び出すことができません、どうしてですか?

マイコード:

オンラインにコントローラ:

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

オンラインに/インデックスビュー:

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

 
<br> 
 

 
<%= render 'posts/post' %>

ポスト/ポストビュー:

<div class="row"> 
 
<%[email protected] 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>

だから、あなたが任意のアイデアを持っている場合は、私にしてください教えてください。あなたの記事/ post.html.erbで

答えて

0

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

posts.onlinesは、1つまたは複数のOnlineオブジェクトのコレクション、およびNOT単一Online記録であるので、あなたは、エラーundefined method 'prix' for Online::ActiveRecord_Associationsに遭遇しました。あなたは<%[email protected] do |post|%> ループ内のすべての特定postためだけ@onlineレコードを選択したい場合は、

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

次に、あなただけの可能性:これにより

は、私は次の値は、(例として)異なる可能性があることを意味し次

記事/ post.html.erbを行う

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

それとも、すべてを表示したい場合のpost.onlinesの各Onlineレコードの値が、その後、あなたは男ああ、あなたは私の救世主です以下

ポスト/ post.html.erb

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

を行うことができます! –

+0

@lilipupu no prob! :) –

関連する問題