Rails link_to 和 button_to 的区别

  • link_to   &   button_to

    link_to 和 button_to 其实还是存在很大区别的,我们可以查看一下源代码。

    源代码:
    https://github.com/rails/rails/blob/master/actionview/lib/action_view/helpers/url_helper.rb

    def link_to(name = nil, options = nil, html_options = nil, &block)
        html_options, options, name = options, name, block if block_given?
        options ||= {}  
    html_options = convert_options_to_data_attributes(options, html_options)
    url = url_for(options)
    html_options["href".freeze] ||= url content_tag("a".freeze, name || url, html_options, &block) end //==================================================================== def button_to(name = nil, options = nil, html_options = nil, &block) html_options, options = options, name if block_given? options ||= {} html_options ||= {} html_options = html_options.stringify_keys
    url = options.is_a?(String) ? options : url_for(options) remote = html_options.delete("remote") params = html_options.delete("params")
    method = html_options.delete("method").to_s method_tag = BUTTON_TAG_METHOD_VERBS.include?(method) ? method_tag(method) : "".freeze.html_safe
    form_method = method == "get" ? "get" : "post" form_options = html_options.delete("form") || {} form_options[:class] ||= html_options.delete("form_class") || "button_to" form_options[:method] = form_method form_options[:action] = url form_options[:'data-remote'] = true if remote
    request_token_tag = if form_method == "post" request_method = method.empty? ? "post" : method token_tag(nil, form_options: { action: url, method: request_method }) else "".freeze end
    html_options = convert_options_to_data_attributes(options, html_options) html_options["type"] = "submit"
    button = if block_given? content_tag("button", html_options, &block) else html_options["value"] = name || url tag("input", html_options) end
    inner_tags = method_tag.safe_concat(button).safe_concat(request_token_tag) if params to_form_params(params).each do |param| inner_tags.safe_concat tag(:input, type: "hidden", name: param[:name], value: param[:value]) end end content_tag("form", inner_tags, form_options) end

    从源代码中可以看出link_to是用url方式跳转的,默认get方式。 而button_to则是生成一个form表单进行提交,默认是post方式。因此他们的使用方法也略有差异,具体使用可以参照github,本章节介绍日常中经常会使用到的一种使用。

    // Routes 
    resources :gbms_dous do
       collection do
         post 'saveSingleDou', to: 'saveSingleDou', as: 'save_single'
         post 'saveMultiDou', to: 'saveMultiDou', as: 'save_multi'
         post 'reloadSingleDou', to: 'reloadSingleDou', as: 'reload_single'
       end
      end
    
    // Page
    <%= link_to 'Reload',  reload_single_gbms_dous_path({id:"#{@dou.DOUID}"}), method: :post , :class=>'btn btn-info btn-sm glyphicon glyphicon-refresh' %>
    
    <%= button_to('Save',save_single_gbms_dous_path,method: :post, params: {:id=>@dou.DOUID}, :class=>'btn btn-info btn-sm')%>
    
    // Controller
     def saveSingleDou
        douID = params[:id]
      …..
      end
    


你可能感兴趣的:(Rails)