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
    


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章