2016-07-22 6 views
0

インデックスビューでヘルパーメソッドを使用しようとしています。私のインデックスでRails 4 - インデックスビューでヘルパーメソッドを使用する方法

、私はこの見解を持っている:

<% policy_scope(Article).sort_by(&:created_at).in_groups_of(2) do |group| %> 

    <% group.compact.each do |article| %> 
    <h4><%= link_to article.title, article %></h4> 
    <small><%= state_notice(article) %></small><br> 
    <small><%= truncate(article.body, :ommission => "...", :length => 250) %></small> 
    <%= link_to 'READ MORE', article_path(article), :class=>"portfolio-item-view" %> 

あるライン:<% = text_for_state(記事)>
%のように私の記事ヘルパーでヘルパーメソッドを持っています

module ArticlesHelper 

    def state_notice(article) 
     if current_user = article.user 
      article.text_for_state(current_state) 
     elsif article.to_be_reviewed and current_user.has_role?(:org_approver) 
      article.text_for_state(current_state) 
     else 
      'test' 
     end 
    end  

     def text_for_state(current_state) 
     case current_state 
     when 'draft' 
     'Private draft' 
     when 'review' 
     'Under pre-publication review' 
     when 'reject' 
     'This article has not been approved for publication' 

     when 'approve' 
     'This article has been approved for publication' 

     when 'publish' 
     'Published' 

     when 'remove' 
     'Removed from publication' 
     end 
    end 

end 

「to_be_reviewed」メソッドは、私の商品ポリシー(私は専門家を使用しています)で定義されています。

私は、ヘルパーで設定された3つのケースのいずれかのテキストを取得する必要があるかどうかをインデックスビューで評価すると考えています。

undefined local variable or method `current_state' for #<#<Class:0x007fba81f217a8>:0x007fba8651f390> 
Did you mean? current_user 

:代わりに、私はというエラーメッセージが表示されます。

誰かが間違っているのを誰でも見ることができますか?

答えて

0

どこからarticleが来ますか?あなたのヘルパーではありません:

module ArticlesHelper 
    def text_for_state(article) 
    if current_user = article.user 
     article.current_state 
    elsif article.to_be_reviewed and current_user.has_role?(:org_approver) 
     article.current_state 
    else 
     'test' 
    end 
    end  
end 
+0

こんにちはサム。私はそれを試みましたが、私は同じ結果を得ました(エラーはありませんが、テキストがあるはずの空白のフィールドのみ)。私はおそらく(記事)私は、インデックスビューは、どの記事を私はヘルパーを使用したい識別するのに十分であると思った。 – Mel

+0

あなたの記事のcurrent_stateは空白になりますか? <%= text_for_state(article)%>を<%= article.current_state%>に置き換えた場合はどうなりますか?まだ空白? –

+0

こんにちはサム - 私はその変更を行うと現在の状態を表示します。状態は空白にすることはできません。これは、状態が新しい記事で初期化されるため(状態=下書きで) – Mel

0

試してください。

def state_notice(article) 
    if current_user = article.user 
    article.text_for_state(article.current_state) 
    elsif article.to_be_reviewed and current_user.has_role?(:org_approver) 
    article.text_for_state(article.current_state) 
    else 
    'test' 
    end 
end 
関連する問題