ruby 在each循环中纠正rescue和next的语法

mepcadol  于 2023-06-05  发布在  Ruby
关注(0)|答案(2)|浏览(194)

我在控制器中有一个相当简单的if else语句,如下所示:

if citation_array.blank?
  flash.now[:error] = "There was a problem saving the publications selected!"
  @user = current_user
  render  'pubmed_search'
else
  citation_array.each do |user_publication|
    begin
      publication = Publication.new
      render_publication(user_publication)
      publication.citation = user_publication
      publication.user_id  = current_user.id
      publication.title = @title
      publication.authors = @authors
      publication.journal = @journal
      publication.year = @year
      publication.volume = @volume
      publication.pages  = @pages
      if publication.save
        next
      end
    rescue
      next
    end
  end

  @user = current_user
  redirect_to current_user
  return false
end

它在citation_array中提供一个id数组,如果有值存在,它会循环遍历它们,保存数组中id找到的每个出版物。render_publication方法示例化了示例变量,所以不用担心这个。
我的问题是。很少有一个id是假的或错误的,所以这个块在那一点上失败了。我想简单地移动到数组中的下一个id,忘记失败的id。我甚至不需要保存一个异常。我是Ruby新手(来自PHP背景)。
我想检查一下这个语法是否正确。我在rails控制台中检查它时遇到问题。

huwehgph

huwehgph1#

如果代码缩进正确,语法错误更容易发现。

if citation_array.blank?
  flash.now[:error] = "There was a problem saving the publications selected!"
  @user = current_user
  render  'pubmed_search'
else
  citation_array.each do |user_publication|
    begin
      publication = Publication.new
      render_publication(user_publication)
      publication.citation = user_publication
      publication.user_id  = current_user.id
      publication.title = @title
      publication.authors = @authors
      publication.journal = @journal
      publication.year = @year
      publication.volume = @volume
      publication.pages  = @pages
      if publication.save
        next
      end
    rescue
      next
    end
  end
  @user = current_user
  redirect_to current_user
  return false
end

语法似乎是正确的。不过,更简单的方法是运行代码。
但是代码中的一些东西是不必要的。在稍微清理代码之后,它看起来像这样,具有相同的功能。

@user = current_user
if citation_array.blank?
  flash.now[:error] = 'There was a problem saving the publications selected!'
  render 'pubmed_search'
else
  citation_array.each do |user_publication|
    begin
      render_publication(user_publication)
      Publication.create!( # create! here so that if something does go wrong, then you're not just ignoring it, but you can log it in your rescue block.
        citation: user_publication,
        user_id: current_user.id,
        title: @title,
        authors: @authors,
        journal: @journal,
        year: @year,
        volume: @volume,
        pages: @pages
        # This hash should be extracted to a method.
      )
    rescue
      # Just doing nothing here is valid syntax, but you should at least log your error.
    end
  end
  redirect_to current_user
  false # This can most likely be omitted as well since not many places care about the return value of a controller action.
end
tkclm6bt

tkclm6bt2#

begin-rescue的语法
begin your code... rescue => e Rails.logger.debug 'Exception is #{e}' end

相关问题