12/08/2018, 17:48

Url Helper in Rails

Link_to link_to(name = nil, options = nil, html_options = nil, &block) public link_to là một Instance Public methods có các thành phần: name: tên của link (<a> name </a>). options: url của link tương ứng với thuộc tính href của thẻ a. html_options: các thuộc tính ...

  1. Link_to

    link_to(name = nil, options = nil, html_options = nil, &block) public

    link_to là một Instance Public methods có các thành phần:

    • name: tên của link (<a> name </a>).
    • options: url của link tương ứng với thuộc tính href của thẻ a.
    • html_options: các thuộc tính khác trong thẻ a như class, id, title,....

    Ví dụ về sử dụng link_to

     <a href="/profiles/1">profiles</a>
     #=> link_to "profile", @profile
    

    hoặc

    <a href="/profiles/1">profiles</a>
    #=> link_to "profile", profile_path(@profile)
    

    khi name là nil

    link_to nil, @profile
    #=> <a href="/profiles/1">/profiles/1</a>
    

    có các html_option:

    <a href="#" class="btn">Delete</a>
    # =>  link_to "Delete", "#", class: "btn"
    

    các data attribute:

    <a href="#" class="btn" data-confirm="Are u sure?">Delete</a>
    # =>  link_to "Delete", "#", class: "btn", data: {confirm: "Are u sure?"}
    

    chứa các thẻ bên trong:

    <a href="#" class="btn" data-confirm="Are You Sure?"><i class="fa fa-trash"></i></a>

    chuyển thành:

    link_to "#", class: "btn", data: {confirm: "AreYou Sure?"} do
        <i class="fa fa-trash"></i>
    end
    

    thêm các query string sau url:

    link_to "search", controller: "searches", query: "ruby on rails"
    #=>  <a href="/searchs?query=ruby+on+rails">search</a>
    

    tùy chọn method:

    <a href="#" data-method="delete">Delete</a>
    #=>  link_to "Delete", "#", method: :delete
    
  2. Button_to

    button_to(name = nil, options = nil, html_options = nil, &block)

    tạo ra một form với duy nhất button với action tạo bởi options

    ví dụ về sử dụng button_to:

      <%= button_to "New", new_post_path %>
      # => "<form method="post" action="/posts/new" class="button_to">
      #      <input value="New" type="submit" />
      #    </form>"
    
      <%= button_to "Delete Image", { action: "delete", id: @image.id },
                                      method: :delete, data: { confirm: "Are you sure?" } %>
      # => "<form method="post" action="/images/delete/1" class="button_to">
      #      <input type="hidden" name="_method" value="delete" />
      #      <input data-confirm='Are you sure?' value="Delete Image" type="submit" />
      #      <input name="authenticity_token" type="hidden" value="10f2163b45388899ad4d5ae948988266befcb6c3d1b2451cf657a0c293d605a6"/>
      #    </form>"
     ```
    
  3. Link_to_if

    link_to_if(condition, name, options = {}, html_options = {}, &block)

    tạo ra link với name và url là options nếu như condition là true, ngược lại trả về name

    ví dụ về sử dụng link_to_if:

      <%=
         link_to_if(@current_user.blank?, "Login", { controller: "sessions", action: "new" }) do
           link_to(@current_user.logged_in?, { controller: "accounts", action: "show", id: @current_user })
         end
      %>
      # chưa login
      # => <a href="/sessions/new/">Login</a>
      # đã login
      # => <a href="/accounts/show/3">my_username</a>
    
0