Rails - 如何替换这些特殊字符?

时间:2013-12-29 05:33:58

标签: jquery ruby-on-rails

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

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

我的链接控制器有以下代码:

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(link_params)

        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_to_merge = params.select { |k, v| ['title', 'description', 'favicon_url'].include?(k) }
        params.require(:link).permit(:content).merge(params_to_merge)

    end

    def find_link
        @link = Link.find(params[:id])
    end
end

在我的 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 %>

我得到的参数如下,除了内容,其他来自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"=>""}

例如,请参阅上面的“标题”或“说明”,我想知道如何用原始外观替换特殊字符?例如,“%3A ”是“”。

任何人都可以帮助我吗?非常感谢。

更新

ActiveSupport的未定义方法`tr':: HashWithIndifferentAccess:0x007ff82585b950

def link_params
    params.each { |k,v| params[k] = CGI.unescape(v) }
    params_to_merge = params.select { |k, v| ['title', 'description', 'favicon_url'].include?(k) }
    params.require(:link).permit(:content).merge(params_to_merge)
end

并将此行标记为错误:

params.each { |k,v| params[k] = CGI.unescape(v) }

3 个答案:

答案 0 :(得分:0)

简短回答是 URI.unescape(str) ,例如:

irb(main):001:0> require 'uri'
=> true
irb(main):002:0> URI.unescape 'http%3A//stackoverflow.com/questions/20815513/rails-typeerror-cant-cast-actioncontrollerparameters-to-text/20815623%3Fnoredirect%3D1%2320815623'
=> "http://stackoverflow.com/questions/20815513/rails-typeerror-cant-cast-actioncontrollerparameters-to-text/20815623?noredirect=1#20815623"

答案很长,那些不应该逃脱。您可以发布simple_form_for创建的HTML吗?此外,为抓取的链接生成的HTML片段确实非常有用。

答案 1 :(得分:0)

您可以使用CGI.unescapeURI.unescape,但请注意URI.unescape因{s>} Ruby 1.9.2问题而导致URI.escape已过时,所以我在这里使用了CGI。

require 'cgi'

params.each do |key, value|
  if value.kind_of?(Hash)
    value.each {|k,v| value[k] = CGI.unescape(v)}
  else
    params[key] = CGI.unescape(value)
  end
end

上面的代码遍历params散列,如果它包含另一个散列,则检查它的每个值。

  1. 如果是这样,它会遍历此哈希并取消其值。

  2. 如果params的值不是哈希值(else语句),则它会将其转移到相关键的params中。

答案 2 :(得分:0)

似乎你正在尝试解码网址

require 'uri'

URI.unescape("Rails%20-TypeError%3A%20can%27t%20cast%20ActionController%3A%3AParameters%20to%20text")

# "Rails -TypeError: can't cast ActionController::Parameters to text"
相关问题