1

I have the following in my controller:

if params[:option] == "1"
      id = params[:id]
      @resultsReceived = true
      begin
       @pwvav_ratings = Pwvav.find_by_id(id).recommendation.split(",")
      #or
       @pwbab_ratings = Pwbab.find_by_id(id).recommendation.split(",")
     #or
       @puvub_ratings = Puvub.find_by_id(id).recommendation.split(",")
     #or
       @tic_ratings   = Tic.find_by_id(id).recommendation.split(",")
     rescue
       redirect_to "/view_api", :flash => { :notice => "Sorry, No records returned for #{id}." }
      end

I am trying to set each of those instance variables (@pwvav_ratings,@pwbab_ratings...) to the find if the find is not nil, so that I can call it in my view. How do I check if..or and if nothing is found, rescue with the flash notice.

4

2 に答える 2

0

Add this to the end of every line. Verifying it's not nil. (Notice ! but you can also use not)

@pwvav_ratings = Pwvav.find_by_id(id).recommendation.split(",") if !Pwvav.find_by_id(id).recommendation.split(",").nil?

Or you can use unless

@pwvav_ratings = Pwvav.find_by_id(id).recommendation.split(",") unless Pwvav.find_by_id(id).recommendation.split(",").nil?
于 2013-03-06T18:59:26.103 に答える
0

Try this.

if params[:option] == "1"
  id = params[:id]
  @resultsReceived = true
  @pwvav_ratings = Pwvav.find_by_id(id)
  #or
  @pwbab_ratings = Pwbab.find_by_id(id)
  #or
  @puvub_ratings = Puvub.find_by_id(id)
  #or
  @tic_ratings   = Tic.find_by_id(id)
  unless @pwvav_ratings || @pwbab_ratings || @puvub_ratings || @tic_ratings
    redirect_to "/view_api", :flash => { :notice => "Sorry, No records returned for #{id}." }
  end
end

You can call .recomendation.split(',') on each variable in your view.

于 2013-03-06T20:19:26.533 に答える