简体   繁体   English

我不能调用我的新变量,为什么不呢?

[英]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.我是 Rails 新手,所以我什至不掌握这种语言的微妙之处。 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.当我尝试将变量“prix”和“部分”调用到帖子索引中时,我遇到了一个错误,它说undefined method 'prix' for Online::ActiveRecord_Associations 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.所以,如果你有任何想法,请告诉我。

In your posts/post.html.erb在你的帖子/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.您遇到了undefined method 'prix' for Online::ActiveRecord_Associations错误,因为posts.onlines是一个或多个Online对象的集合,而不是单个Online记录。

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.each do |post|%>循环中的每个特定post选择@online记录,那么您可以执行以下操作

posts/post.html.erb帖子/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或者,如果您想在post.onlines显示每个Online记录的所有prix值,那么您可以执行以下操作

posts/post.html.erb帖子/post.html.erb

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM