[英]Rails -TypeError: can't cast ActionController::Parameters to text
I'm developing a website using jQuery Preview to fetch the title, description or favction_url of any link. 我正在使用jQuery Preview开发一个网站,以获取任何链接的标题,描述或favction_url。
jQuery Preview: https://github.com/embedly/jquery-preview/ jQuery预览: https : //github.com/embedly/jquery-preview/
My links controller has this code: 我的链接控制器具有以下代码:
class LinksController < ApplicationController
before_action :find_link, :only => [: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
And in my views/links/new.html.erb has this code: 在我的views / links / new.html.erb中有以下代码:
<script>
// 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;
});
</script>
<h1>Share Link</h1>
<%= simple_form_for @link do |f| %>
<%= f.input :content, :input_html => {:id => "url"} %>
<div class="selector-wrapper"></div>
<%= f.button :submit, :disable_with => "Submiting...", :class => "btn btn-primary btn-large" %>
<% end %>
But when I click submit, I got an error : 但是,当我单击提交时,出现错误:
TypeError: can't cast ActionController::Parameters to text: INSERT INTO "links" ("content", "created_at", "description", "favicon_url", "title", "updated_at") VALUES (?, ?, ?, ?, ?, ?) TypeError:无法将ActionController :: Parameters转换为文本:INSERT INTO“链接”(“内容”,“ created_at”,“描述”,“ favicon_url”,“标题”,“ updated_at”)值(?,?,? ,?,?,?)
And it marked this line of links controller code: 并标记了这行链接控制器代码:
if @link.save
Can anyone help me? 谁能帮我? Thank you.
谢谢。
The parameters I got is like following, except content , others are from jQuery Preview: 我得到的参数如下所示,除了content ,其他参数来自jQuery Preview:
Parameters: {"utf8"=>"✓",
"authenticity_token"=>"NDqZbleBkEEZzshRlTM+d6GZdVEGAoO1W/mp7B68ZZ0=",
"link"=>{"content"=>"http://stackoverflow.com/questions/20815513/rails-typeerror-cant-cast-actioncontrollerparameters-to-text/20815623?noredirect=1#20815623"},
"commit"=>"submit",
"original_url"=>"http%3A//stackoverflow.com/questions/20815513/rails-typeerror-cant-cast-actioncontrollerparameters-to-text/20815623%3Fnoredirect%3D1%2320815623",
"url"=>"http%3A//stackoverflow.com/questions/20815513/rails-typeerror-cant-cast-actioncontrollerparameters-to-text",
"type"=>"html",
"provider_url"=>"http%3A//stackoverflow.com",
"provider_display"=>"stackoverflow.com", "provider_name"=>"Stackoverflow",
"favicon_url"=>"http%3A//cdn.sstatic.net/stackoverflow/img/favicon.ico",
"title"=>"Rails%20-TypeError%3A%20can%27t%20cast%20ActionController%3A%3AParameters%20to%20text",
"description"=>"I%27m%20developing%20a%20website%20using%20jQuery%20Preview%20to%20fetch%20the%20title%2C%20description%20or%20favction_url%20of%20any%20link.",
"thumbnail_url"=>"http%3A//cdn.sstatic.net/stackoverflow/img/apple-touch-icon@2.png%3Fv%3Dfde65a5a78c6",
"author_name"=>"",
"author_url"=>"",
"media_type"=>"",
"media_html"=>"",
"media_width"=>"",
"media_height"=>""}
In your create
action you're trying to assign link_params
hash as value for content
which expects text. 在您的
create
操作中,您试图将link_params
哈希值分配为需要文本的content
值。
With the call to Link.new(...)
passing in attributes you are mass assigning attributes, and with Rails4 strong parameters you need to add all the attributes to the permit
list that you will be mass assigning. 通过对传递属性的
Link.new(...)
的调用,您正在批量分配属性,而使用Rails4强大的参数,您需要将所有属性添加到将要批量分配的permit
列表中。
Update the create
and link_params
method definition as follows: 如下更新
create
和link_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
Update: Merge certain attributes from the parameter hash and merge them to params[:link]
更新:合并参数哈希中的某些属性,并将它们合并为
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.