私はレールが新しく、このラングゲージの微妙ささえも習得しません。 変数「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で
を行うことができます! –
@lilipupu no prob! :) –