我知道如何使用Rails 3.2.18的pismo gem保存页面标题和图标。我的问题是我怎样才能用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
假设您的旧代码在Rails 3.2中工作,您应该能够像以前一样创建文档,并在调用@bookmark = current_user.bookmarks.build(bookmark_params)
后设置@bookmark.title
和@bookmark.favicon
# 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