ROR 多对多关联,索引页

ROR Many to many association ,index page

我是 rails 的新手,我正在构建这个应用程序,它有 3 个模型 Customer、Ad、CustomerAd 其中看到的是参数 customer_ad 我希望 CustomerAd 显示所有广告并让客户观看它们 因此,当客户点击其中一个广告列表(显示广告)时 CustomerAd 将更新为 true
我正在使用:

Customer
  has_many :customer_ads
  has_many :ads through: :customer_ads

Ad
  has_many :customer_ads
  has_many :customers through: :customer_ads

CustomerAd
  belongs_to :customer
  belongs_to :ad

views/ads/show

%body    
        %div{:align => "auto", :dir => "auto"}
          %p#notice= notice
          %p
            %strong= t('activerecord.attributes.ad.business_id')
            = @ad.business_id
          %p
            %strong= t('activerecord.attributes.ad.short_description')
            = @ad.short_description
          %p
            %strong= t('activerecord.attributes.ad.long_description')
            = @ad.long_description
          %p
            %strong= t('activerecord.attributes.ad.start_date')
            = @ad.start_date
          %p
            %strong= t('activerecord.attributes.ad.end_date')
            = @ad.end_date  
          = link_to t('views.ad.show.edit'), edit_ad_path(@ad)
          |
          \#{link_to t('views.ad.show.back'), customer_ads_path}

查看 customer_ad 索引和控制器

              %tbody                  
                - @ads.each do |ad| 
                  - if @customer_ad.seen == true
                    %tr#btn.seen{"data-link" => "#{ad_path(ad)}"}
                      %td
                        = ad.short_description
                        = ad.long_description

                  - else
                    %tr#btn.unseen{"data-link" => "#{ad_path(ad)}"}
                      %td
                        = ad.short_description
                        = ad.long_description

CustomerAdsController

class CustomerAdsController < ApplicationController
  before_action :set_customer_ad, only: [:show, :edit, :update, :destroy]    
  def index        
    @ads = Ad.all
  end

  private
    def set_customer_ad
      @customer_ad = CustomerAd.find(params[:id])
    end


    def customer_ad_params
      params.require(:customer_ad).permit(:customer_id, :ad_id, :bought, :seen)
    end
end

广告控制器

class AdsController < ApplicationController
  before_action :set_ad, only: [:show, :edit, :update, :destroy] 

  def show
   ad = Ad.find(params[:id])
   current_customer.customer_ads.where(ad_id: ad.id).update(seen: true)
  end 

 def new
  @ad = Ad.new
 end

def edit
end

def create
  @ad = Ad.new(ad_params)    

  respond_to do |format|
    if @ad.save
      format.html { redirect_to customer_ads_url }
      format.json { render :show, status: :created, location: @ad }
    else 
      format.html { render :new }
      format.json { render json: @ad.errors, status: :unprocessable_entity }
    end
  end
end

def update
  respond_to do |format|
    if @ad.update(ad_params)
      format.html { redirect_to @ad, notice: 'Ad was successfully updated.' }
      format.json { render :show, status: :ok, location: @ad }
    else
      format.html { render :edit }
      format.json { render json: @ad.errors, status: :unprocessable_entity }
    end
  end
end

def destroy
  @ad.destroy
  respond_to do |format|
    format.html { redirect_to ads_url, notice: 'Ad was successfully destroyed.' }
    format.json { head :no_content }
  end
end

private
  def set_ad
    @ad = Ad.find(params[:id])
  end

  def ad_params
    params.require(:ad).permit(:business_id, :short_description, :long_description, :start_date, :end_date, :filter_id, :maximum_number_of_customers_targeted, :tokens_allocated_for_reading, :tokens_allocated_for_purchasing)
  end
end
def show
  ad = Ad.find params[:id]
  current_customer.cusomer_ads.where(ad_id: ad.id).update(seen: true)
end

您将针对 current_customer 和该特定广告更新 customer_ad。 在 current_customer,您将登录客户。

在索引页上,您将执行类似

的操作
- @ads.each do |ad|
    - @customer_ad = ad.customer_ads.where(customer_id: current_customer.id).first
    - if @customer_ad.present? && @customer_ad.seen == true
      %tr#btn.seen{"data-link" => "#{ad_path(ad)}"}
        %td
          = ad.short_description
          = ad.long_description

    - else
      %tr#btn.unseen{"data-link" => "#{ad_path(ad)}"}
        %td
          = ad.short_description
          = ad.long_description

来自代码 我认为您正在放置代码以在 AdsController 中将 seen 更新为 true 但让用户 link 显示 CustomerAdsControoler 显示页面的页面

在 CustomerAdsController 中放置以下代码

def show
 ad = Ad.find(params[:id])
 current_customer.customer_ads.where(ad_id: ad.id).update(seen: true)
end 

使行可点击

  $("tr[data-link]").click(function() {
      window.location = $(this).data("link")
    })