1

我的控制器中有以下内容:

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

如果 find 不为零,我正在尝试将每个实例变量(@pwvav_ratings@pwbab_ratings...)设置为 find,以便我可以在我的视图中调用它。我如何检查是否..或如果没有找到,请使用 Flash 通知进行救援。

4

2 回答 2

0

尝试这个。

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

您可以调用.recomendation.split(',')视图中的每个变量。

于 2013-03-06T20:19:26.533 回答
0

将此添加到每一行的末尾。验证它不是零。(注意!,但您也可以使用not

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

或者你可以使用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 回答