开发者

How can I make the html output from button_to have single quotes instead of double?

I've got this line:

<%= button_to 'Post', newpost_path(:type => 'short_note') %>

which outputs this:

<form method="post" action="/posts/newpost?type=short_note" class="button-to"><div><input type="submit" value="Add" /></div></form>;

But I need the output to have single quotes, not double. How can I do this?

<form method='post' action='/posts/newpost?type=short_note' class='button-to'><div>开发者_C百科;<input type='submit' value='Add' /></div></form>;


Try this:

<%= (button_to 'Post', newpost_path(:type => 'short_note')).gsub('"', '\'') %>


create a file in rails_root/lib called url_helper_overrides.rb that simply overrides the button to method. Or you could add the button_to method below to your application_helper. This method is taken straight from the UrlHelper source in Rails (Github Link), I simply replaced the double quotes with single quotes.

module ActionView
  module Helpers
    module UrlHelper

      def button_to(name, options = {}, html_options = {}) 
        html_options = html_options.stringify_keys
        convert_boolean_attributes!(html_options, %w( disabled ))

        method_tag = ''
        if (method = html_options.delete('method')) && %w{put delete}.include?(method.to_s)
          method_tag = tag('input', :type => 'hidden', :name => '_method', :value => method.to_s)
        end 

        form_method = method.to_s == 'get' ? 'get' : 'post'

        remote = html_options.delete('remote')

        request_token_tag = ''
        if form_method == 'post' && protect_against_forgery?
          request_token_tag = tag(:input, :type => "hidden", :name => request_forgery_protection_token.to_s, :value => form_authenticity_token)
        end 

        url = options.is_a?(String) ? options : self.url_for(options)
        name ||= url 

        html_options = convert_options_to_data_attributes(options, html_options)

        html_options.merge!("type" => "submit", "value" => name)

        ("<form method='#{form_method}' action='#{html_escape(url)}' #{"data-remote='true'" if remote} class='button_to'><div>" +
          method_tag + tag("input", html_options) + request_token_tag + "</div></form>").html_safe
      end
    end
  end
end


I think the only way is to gsub(/"/, "'") but this is really ugly

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜