活动管理自定义页面中有几行代码被转义 page_action

Few line of codes are geting escaping at active admin custom page page_action

在使用 Active Admin-> Custom Page -> page_action 时,我在遵循代码中指出的代码块时遇到困难。我试图通过异常测试代码的可达性。我不明白如果我将异常放在代码中的位置 2 为什么不会得到异常?

page_action :add_event, method: :post do
  blogit_posts=params["featured_post"]["blog_posts_attributes"].values.map { |s| 
    {
      :blogit_post_id=>s["blogit_post_id"],
      :id=> s["id"] ? s["id"] : s["i/nd"],
      :priority=>s["priority"],
      :_destroy=>s["_destroy"]
    } 
  }

  blogit_posts.each do |blog_hash|

      #raise "unknown" <-- 1. if i put here, I get exception for it

      if blog_hash[:id]
        b=BlogPost.find_by_id(blog_hash[:id].to_i)
      else if blog_hash[:blogit_post_id]
        b=BlogPost.find_by_blogit_post_id(blog_hash[:blogit_post_id].to_i)
      end

      #raise "unknown" <-- 2. if i put here, I **DO NOT** get exception for it

      if blog_hash[:_destroy] && blog_hash[:_destroy]=="1"   

        b.is_featured=false # <--- trying to fix this code block 

      else
        b.is_featured=true
      end
    b.priority =blog_hash[:priority].to_i
    b.save
  end
end
 redirect_to admin_featuredpost_path, notice: "Featurd post updated "
end

如果您尝试适当地格式化代码,您会立即发现错误:

if blog_hash[:id]
    b=BlogPost.find_by_id(blog_hash[:id].to_i)
else 
    if blog_hash[:blogit_post_id]
      b=BlogPost.find_by_blogit_post_id(blog_hash[:blogit_post_id].to_i)
    end

    ???

也就是说,你打开了if,也就是下面15行关闭了。应该将 elsif 用于意大利面条 ifs 或正确关闭嵌套 ifs.

您可能遇到了此块的问题:

if blog_hash[:id]
  b=BlogPost.find_by_id(blog_hash[:id].to_i)
else if blog_hash[:blogit_post_id]
  b=BlogPost.find_by_blogit_post_id(blog_hash[:blogit_post_id].to_i)
end

我相信你的意思是写这个:

if blog_hash[:id]
  b=BlogPost.find_by_id(blog_hash[:id].to_i)
elsif blog_hash[:blogit_post_id]
  b=BlogPost.find_by_blogit_post_id(blog_hash[:blogit_post_id].to_i)
end

请注意elsif