2016-04-26 3 views
0

私はRuby on Rails 4アプリを持っています。Rails 4 - 複雑な条件付きビューのMVCを考慮し、ホームページ上で尊重する

基本モデルはユーザーと取引です。 各ユーザーは多くの取引に参加することができ、各取引に参加した回数(ログイン時)をカウントします。 取引とユーザーには、UserDealテーブルを使用して、多数の取引があります。

コントローラやモデルメソッドにコードを書き込もうとしましたが、動作していないコードが残っていましたが、コントローラー、モデル、懸念事項のどこかに入れなければならないと感じていますこのビューのようにはそうではありません)。

基本的にはこのホームページ上の、私はそのカードに書き込み電流取引のリストを表示し、各取引について

  • ユーザーがサインインしているされている場合:ユーザーが参加している時間数契約の中で

  • ユーザーがログインしていない場合: 'who are you'のような文章ですか? UserDealテーブル内:

重要「は、この取引に参加して行く上で来るuserdealラインは、ユーザID = 4は、契約のページに行く2、ユーザーID = 4ときにのみ=取引のために表示されます= 2何かをする。この行/オブジェクトの前には存在しません。

ホームページビュー(カードのリストを持つブロック)

<% @deals.each do |deal| %> 

    <li class="card <%= deal.id %>"> 
     <%= @deal_number_of_participations_in_deal  
     # User is signed-in 
     if user_signed_in? 
     @userdeal = UserDeal.where('user_id = ? AND deal_id = ?', current_user.id, deal.id).take 
     if @userdeal.nil? 
      @deal_number_of_participations_in_deal = 'you never did anything in this deal' 
     # if user signed in and has participated in the deal 
     else 
      @deal_number_of_participations_in_deal = @userdeal.nb_participations_past_week - @userdeal.nb_participations_two_weeks_ago 
     end 

     # user is an anonymous visitor 
     else 
     @deal_number_of_participations_in_deal = 'who are you ? come on go participate on this deal' 
     end  

     %> 

    <div class="card-content" id="operation_<%= deal.id %>"> 
     here is the content of the card: <%= deal.content %> 

    </li> 
<% end %> 

モデル

class Deal < ActiveRecord::Base 
    has_many :user_deals,   dependent: :destroy 
    has_many :users,    through: :user_deals 
end 

class User < ActiveRecord::Base 
    has_many :user_deals   
    has_many :deals,    through: :user_deals 
end 
class UserDeal < ActiveRecord::Base 
    belongs_to :user,   :foreign_key => 'user_id' 
    belongs_to :deal,   :foreign_key => 'deal_id' 
end 

私の質問は:どこ私はその下のコードを配置する必要があり、すべての今日ですMVCを尊重し、Rails-y(再利用可能、モジュラーコード...)とする観点からは? DealやUserのモデルやコントローラに入れてみましたが、動作しませんでした。私は今日彼らが見る側にいるときにのみ働く。

<%= @deal_number_of_participations_in_deal  
      # User is signed-in 
      if user_signed_in? 
      @userdeal = UserDeal.where('user_id = ? AND deal_id = ?', current_user.id, deal.id).take 
      if @userdeal.nil? 
       @deal_number_of_participations_in_deal = 'you never did anything in this deal' 
      # if user signed in and has participated in the deal 
      else 
       @deal_number_of_participations_in_deal = @userdeal.nb_participations_past_week - @userdeal.nb_participations_two_weeks_ago 
      end 

      # user is an anonymous visitor 
      else 
      @deal_number_of_participations_in_deal = 'who are you ? come on go participate on this deal' 
      end  

      %> 

HomepageController

class StaticPagesController < ApplicationController 
    def home  
    @deals = deals.featured_on_hp  
    respond_to do |format| 
     format.html # home.html.erb 
     format.json { render json: @deals } 
     format.xml { render xml: @deals } 
    end 
    end 
end 

ディールモデル

scope :featured_on_hp, -> { order(deal_end_date: :asc) } 

EDIT REQとして

詳細コメントに入れられた。例えば

私はHomepagecontroller内のコードのブロックを追加した場合、私はこの

class StaticPagesController < ApplicationController 
    def home  
    @deals = deals.featured_on_hp 

# User is signed-in 
if user_signed_in? 
    @userdeal = UserDeal.where('user_id = ? AND deal_id = ?', current_user.id, deal.id).take 
     if @userdeal.nil? 
     @deal_number_of_participations_in_deal = 'you never did anything in this deal' 
     # if user signed in and has participated in the deal 
     else 
     @deal_number_of_participations_in_deal = @userdeal.nb_participations_past_week - @userdeal.nb_participations_two_weeks_ago 
     end 

# user is an anonymous visitor 
else 
    @deal_number_of_participations_in_deal = 'who are you ? come on go participate on this deal' 
end 


respond_to do |format| 
    format.html # home.html.erb 
    format.json { render json: @deals } 
    format.xml { render xml: @deals } 
    end 
end 
end 

を取得し、私はこのエラーを取得する:

undefined local variable or method `deal' for #<HomepageController:0x00xxxxxxx> 
+0

質問は何ですか? –

+0

質問を編集=>最後を参照 – Mathieu

+0

私はそれが簡略化することができると信じて、デコレータに置く –

答えて

1

このライン

@deals = deals.featured_on_hp 

はする必要があります

あなたが契約してユーザーを持っていて、少しの手順でビューを簡素化することにより、取引

current_user.deals.any?{|d| d.id == deal.id} 

スタートからユーザーを取得することができますmany_to_many関係があるので、あなたが物事を行うことができる場所

、あなたが表示されます改善するために異なっている。 1つのステップは、これは上の向上を図ることができるが、それはビュークリーナーを作る

module DealHelper 
    def user_deal_message(user, deal) 
    if user 
     msg = signed_in_user_deal_message(user, deal) 
    else 
     msg = 'who are you ? come on go participate on this deal' 
    end 
    msg 
    end 

    def signed_in_user_deal_message(user, deal) 
    user_deal = user.deals.select{|d| d.id == deal.id} 
    if user_deal.any? 
     user_deal_participations(user, user_deal.first) 
    else 
     'you never did anything in this deal' 
    end 
    end 

    def user_deal_participations(user, deal) 
    user.deals.nb_participations_past_week - user_deal.nb_participations_two_weeks_ago 
    end 
end 

<li class="card <%= deal.id %>"> 
    <%= user_deal_message(@user, deal) %> 
    <div class="card-content" id="operation_<%= deal.id %>"> 
    here is the content of the card: <%= deal.content %> 
</li> 

ヘルパー/ deal_helper.rb ...ヘルパーメソッドへのliタグにロジックを置くことであろう。また、部分的な使用を検討してください。ページに多くのことがある場合、ビューを部分化することは、特に複数の人が作業しているときや追加するときに、保守性を向上させるのに役立ちます。

+0

私はそれをテストしています。 'user'を使うと、これはuser_signed_inならdevise経由でチェックしているのと似ています。ユーザーがサインインしているかどうかをチェックする機能もあります。 – Mathieu

+0

コントローラ変数にuser変数を設定します。 @user = user_signed_in? ? current_user:なし。その後、あなたはビューでそれを確認することができます、私は私の答えを更新しました。私はあなたのビューとコントローラの完全なコンテキストを知らないので、あなたはそれを使いこなす必要があります。 – margo

関連する問題