Rails -TypeError:无法将ActionController :: Parameters强制转换为文本

我正在开发一个使用jQuery Preview的网站来获取任何链接的标题,描述或favction_url。

jQuery预览: https : //github.com/embedly/jquery-preview/

我的链接控制器有这个代码:

class LinksController  [:edit, :update, :destroy] def index @links = Link.all end def new @link = Link.new end def create @link = Link.new(:content => link_params, :title => params[:title], :description => params[:description], :favicon_url => params[:favicon_url]) if @link.save redirect_to root_path else render :new end end def edit end def update if @link.update_attributes(link_params) redirect_to root_path else render :edit end end def destroy @link.destroy redirect_to root_path end private def link_params params.require(:link).permit(:content) end def find_link @link = Link.find(params[:id]) end end 

在我的views / links / new.html.erb中有这样的代码:

  // Set up preview. $('#url').preview({key:'key'}) // On submit add hidden inputs to the form. $('form').on('submit', function(){ $(this).addInputs($('#url').data('preview')); return true; });  

Share Link

{:id => "url"} %>
"Submiting...", :class => "btn btn-primary btn-large" %>

但是当我点击提交时,我收到了一个错误:

TypeError:无法将ActionController :: Parameters强制转换为text:INSERT INTO“links”(“content”,“created_at”,“description”,“favicon_url”,“title”,“updated_at”)VALUES(?,?,? ,?,?,?)

它标记了这一行链接控制器代码:

 if @link.save 

谁能帮我? 谢谢。

我得到的参数如下,除了内容 ,其他参数来自jQuery Preview:

 Parameters: {"utf8"=>"✓", "authenticity_token"=>"NDqZbleBkEEZzshRlTM+d6GZdVEGAoO1W/mp7B68ZZ0=", "link"=>{"content"=>"http://sofzh.miximages.com/jquery/20815623%3Fv%3Dfde65a5a78c6", "author_name"=>"", "author_url"=>"", "media_type"=>"", "media_html"=>"", "media_width"=>"", "media_height"=>""} 

在您的create操作中,您尝试将link_params哈希值指定为期望文本的content值。

通过调用Link.new(...)传递属性,您可以批量分配属性,使用Rails4强参数,您需要将所有属性添加到您将批量分配的permit列表中。

更新createlink_params方法定义,如下所示:

 # app/controllers/links_controller.rb def create @link = Link.new(link_params) if @link.save redirect_to root_path else render :new end end private def link_params params.require(:link).permit(:content, :title, :description, :favicon_url) end 

更新:合并参数哈希中的某些属性并将它们合并到params[:link]

 # app/controllers/links_controller private def link_params # Select parameters you need to merge params_to_merge = params.select { |k, v| ['title', 'description', 'favicon_url'].include?(k) } params.require(:link).permit(:content).merge(params_to_merge) end