rails4,强烈的params pismo

时间:2014-08-12 01:58:49

标签: ruby-on-rails-4 nokogiri strong-parameters

我知道如何使用带有Rails 3.2.18的pismo gem保存页面标题和favicon。我的问题是如何使用Rails 4强大的参数来做同样的事情,这对我来说有点混乱。

任何帮助将不胜感激,或者如果您现在知道更好的宝石或更好的方法来保存页面标题和图标将有助于2. :)

# Rails 3.2.16
 def create
    require 'pismo'
    bookmark = Bookmark.new(params[:bookmark])
    bookmark.user_id = current_user.id

    doc = Pismo::Document.new(bookmark.link)
    @title = doc.title
    @favicon = doc.favicon
    bookmark.title = @title
    bookmark.favicon = @favicon

    flash[:error] = "your bookmark wasn't save" unless bookmark.save
    redirect_to current_user

 end

这是我的rails 4控制器来保存书签,但我不确定在哪里或如何实现pismo gem。

# Rails 4.1.4
def create
   @bookmark = current_user.bookmarks.build(bookmark_params)
   if @bookmark.save
     flash[:success] = "Bookmark save"
     redirect_to statics_home_path
   else
     redirect_to statics_home_path
  end
end

私有

def bookmark_params
  params.require(:bookmark).permit(:link, :title)
end

2 个答案:

答案 0 :(得分:0)

假设您的旧代码在Rails 3.2中有效,您应该可以像以前一样创建文档,并在调用@bookmark.title后设置@bookmark.favicon@bookmark = current_user.bookmarks.build(bookmark_params)

答案 1 :(得分:0)

# this works with Pismo
# I'm sure it'll work with Nokogiri


def create
  require 'pismo'
  doc = Pismo::Document.new(bookmark_params[:link])
  @bookmark = current_user.bookmarks.build(bookmark_params)
  @bookmark.title = doc.title.to_s

  if @bookmark.save
    #flash[:success] = "Bookmark save"
    redirect_to statics_home_path
  else
    redirect_to statics_home_path
    #render 'statics/home'
  end
end