Jquery Rails-TypeError:can';t将ActionController::参数转换为文本
我正在开发一个网站,使用jQuery预览获取任何链接的标题、描述或favction_url jQuery预览: 我的链接控制器具有以下代码:Jquery Rails-TypeError:can';t将ActionController::参数转换为文本,jquery,ruby-on-rails,Jquery,Ruby On Rails,我正在开发一个网站,使用jQuery预览获取任何链接的标题、描述或favction_url jQuery预览: 我的链接控制器具有以下代码: class LinksController < ApplicationController before_action :find_link, :only => [:edit, :update, :destroy] def index @links = Link.all end def new
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
<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 %>
有人能帮我吗?多谢各位
我得到的参数如下所示,除了内容,其他参数来自jQuery预览:
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"=>""}
在
create
操作中,您试图将link_params
hash指定为需要文本的内容的值
通过调用Link.new(…)
传入属性,您将大量分配属性,而通过Rails4强参数,您需要将所有属性添加到将大量分配的许可
列表中
更新创建
和链接参数
方法定义,如下所示:
# 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
谢谢,但我得到的参数不是这样的。除了:content,其他都来自jQuery预览,它们不在:link下。为了便于阅读,我把我得到的所有参数都放在上面。我想知道如何为除params[:link][:content]之外的其他参数编写代码。您需要将所有参数合并到params[:link]
,以便在批量分配中允许它们。如果您无法修改视图以将这些参数包含在链接表单中,则应在控制器中完成。非常感谢。但是如何将所有参数合并到params[:link]?我找到了一些帖子,但我不知道如何应用到我的应用程序中。对不起,我是一个非常初学者。@user3087000,请查看更新。请注意,params\u to\u merge
hash应包括允许批量分配所需的所有属性,因此请确保在params.select中选择所需的所有属性。
# 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