【发布时间】:2018-09-15 01:28:25
【问题描述】:
我是 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
观看次数/广告/节目
%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
客户广告控制器
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
【问题讨论】:
-
使用正确的型号名称而不是 A、Bs 等。使用代码突出显示和格式化内容,以便人们能够理解您的要求。
-
我修改了它@MuhammadFaisalIqbal
-
不太清楚你到底想要什么。以及确切的问题是什么
-
您想在用户点击节目时将 customer_ad 更新为 seen true 吗?
-
确切的问题是 seen 没有更新显示广告操作! @DeepakMahakale
标签: ruby-on-rails ruby ruby-on-rails-3 ruby-on-rails-4 ruby-on-rails-3.2