1

の後Post.all、カスタム属性を持つ投稿オブジェクトの JSON 配列を返そうとしています。ここに私が持っているものがあります:

@posts = Post.includes(:profil).page(params[:page]).order('created_at DESC')
render json: @posts.to_json(:include => :profil,
                            :methods => [:image_url, :vote_modificator])

Profilこのメソッドは、カスタム属性を含む投稿のリストを正しく返しますimage_urlVoteおよびに属するモデルがProfilありPostます。

class Vote < ActiveRecord::Base
  attr_accessible :value
  belongs_to :profil
  belongs_to :post
end

Vote各投稿に、それ自体に対応する値と、そのcurrent_userように定義された値を挿入したいapplication_controller.rb:

  def current_user
      @current_user ||= User.find_by_auth_token!(cookies[:auth_token]) if cookies[:auth_token] 
  end

  helper_method :current_user

にメソッドを追加しようとしましたto_jsonが、モデルで定義されたメソッドはこのヘルパーにアクセスできません。私の問題を解決するための解決策はありますか?

4

0 に答える 0