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

时间:2016-05-10 14:46:57

标签: javascript jquery sql ruby-on-rails ruby

我是铁杆的新人,所以我甚至不掌握这种语言的微妙之处。 当我尝试调用我的变量" prix"我遇到一个错误,说undefined method 'prix' for Online::ActiveRecord_Associations。和"部分"进入后期指数。 我可以这么做吗?

我的代码:

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
&#13;
&#13;
&#13;

Onlines / Index view:

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

<br>

<%= render 'posts/post' %>
&#13;
&#13;
&#13;

帖子/帖子视图:

&#13;
&#13;
<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>
&#13;
&#13;
&#13;

所以,如果您有任何想法,请告诉我。

1 个答案:

答案 0 :(得分:0)

在你的帖子/ post.html.erb

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

您遇到错误undefined method 'prix' for Online::ActiveRecord_Associations,因为posts.onlines是一个或多个Online个对象的集合,而不是一个Online记录。

由此,我的意思是以下值可能不同(作为示例):

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

如果您只想为@online循环中的每个特定post选择<%-@posts.each do |post|%> 记录,那么您可以执行以下操作

文章/ post.html.erb

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

或者,如果您想在prix中显示每个Online记录的所有post.onlines值,那么您可以执行以下操作

文章/ post.html.erb

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