简体   繁体   中英

Conditional statement fails Rails 4

I am developing a rails app that has Pins that are assigned a Category when created. The app has 16 Categories in total. All Pins are displayed on a main index page ( except Category 16 ) and a user can click a Pin which will take them to a show page of the @Pin. The Show page has the @Pin and a Random Pin selected from all Pins in the db ( except Category 16 ). The mentioned actions above are defined in the pins controller and function without error.

The problem is when Category 16 is selected and the user proceeds to a Pins show page the Random Pin should be a Pin from Category 16 Only, but it is being selected from all the Pins in the db!

I have set variables and Conditional statements in the index, and show actions to achieve the above, please check my comments in pins controller show-action. Problem code persists at comment ====SOMETHING WRONG!====

Please help i am relatively new to rails I'm not sure what i am doing wrong, i have spent hours trying all sorts but nothing has worked.

pins_controller index action

def index
if params[:category].blank?
  @restricted = Category.find(16) # category id 16 set as var @restricted used to exclude cat 16 from index page in line below.
  @pins = Pin.where.not(category: @restricted).all.order("created_at DESC").paginate(page: params[:page], per_page: 20)
else
  @category_id = Category.find_by(name: params[:category]).id
  @pins = Pin.where(category_id: @category_id).order("created_at DESC").paginate(page: params[:page], per_page: 20)
end 

pins_controller show-action THE PROBLEM

def show
@pin.increment_view_count
@show_main_cat = Category.where.not(16) # all categories except category id 16 set as var @show_main_cat
if @show_main_cat.present? 
  @restricted = Category.find(16) # category id 16 set as var @restricted.
  @random_pin = Pin.where.not(id: @pin, category: @restricted).order("RANDOM()").first # select random pin from all categories except 16 + not @pin.
end
@show_restricted_cat = Category.find(16) # category 16 set as var @show_restricted_cat < ==========SOMETHING WRONG!==============
if @show_restricted_cat.present?
  @restricted = Category.where.not(16) # all categories except 16 set as var @restricted. 
  @random_pin = Pin.where.not(id: @pin, category: @restricted).order("RANDOM()").first # Only select random pin from category 16 + not @pin.
end

@reviews = Review.where(pin_id: @pin.id).order("created_at DESC").limit(5)
if @reviews.blank?
  @avg_review = 0
else
  @avg_review = @reviews.average(:rating).round(2)
end

This line is not what you expected

@random_pin = Pin.where.not(id: @pin, category: @restricted).order("RANDOM()").first # Only select random pin from category 16 + not @pin.

It will select a random pin from category 16 OR id not @pin (What you expect I think from category 16 AND id not @pin ). Change it to:

@random_pin = Pin.where.not(category: @restricted).where.not(id: @pin).order("RANDOM()").first

Have fun!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM