有关Rails

时间:2015-07-09 01:03:25

标签: ruby-on-rails ruby ruby-on-rails-4

执行摘要

  1. 通过暴力猜测来阅读API并使用“link_to”。
  2. 提供API和描述,并试图弄清楚我的蛮力猜测是如何起作用的??
  3. ==结束

    我正在尝试学习“如何在Rails中读取API文档”。一个给我带来很多麻烦的特殊功能是link_to功能。简单明了,我只需要学习如何“阅读api函数”,这个问题与此有关。

    网址:http://apidock.com/rails/v4.0.2/ActionView/Helpers/UrlHelper/link_to

    当前文档详细介绍了以下内容

    1. link_to(body, url, html_options = {})
      # url is a String; you can use URL helpers like
      # posts_path
    
    2. link_to(body, url_options = {}, html_options = {})
      # url_options, except :method, is passed to url_for
    
    3. link_to(options = {}, html_options = {}) do
      # name
    end
    
    4. link_to(url, html_options = {}) do
      # name
    end
    

    最终,我想添加一大堆HTML样式,并使用DO块将AJAX用于我的link_to功能。

    这是有效的,但我只是不明白为什么使用选项#4而根据原型不使用选项#3?

    此工作:

    <%= link_to session_path(@account.id), {remote: true, :method => :delete, id: "abc",style: "display:inline-block; top: 20px; color: red; outline: none;" } do %>
                <span class="glyphicon glyphicon-log-out" style: "display:inline-block; top: 20px; color: red; outline: none;"></span>
                <span id="nav_loginout_action" style= "top: 20px; color: none; outline: none;display:inline-block;margin-left: 5px;">
                 Logout </span>
                <% end %>
    

    这不是

    但是,如果您注意到{remote:true, method => delete}用于OPTIONS,{id: "abc",style: "display:inline-block; top: 20px; color: red; outline: none;"}用于HTML_OPTIONS

    <%= link_to session_path(1), {remote: true, :method => :delete}, {id: "abc",style: "display:inline-block; top: 20px; color: red; outline: none;" } do %>
                    <span class="glyphicon glyphicon-log-out" style: "display:inline-block; top: 20px; color: red; outline: none;"></span>
                    <span id="nav_loginout_action" style= "top: 20px; color: none; outline: none;display:inline-block;margin-left: 5px;">
                     Logout </span>
                    <% end %>
    

    任何人都可以解释如何阅读Rails文档吗?

    特别是

    这是困扰我的:

    In the documentation OPTIONS = {} is specifically defined as ".... :data, :method and remote:true. 
    

    在选项4中,它需要一个HTML_OPTION(**不是选项,在选项#3 **中特别要求)。请参阅提供的URL链接

    ONLY,并重复OPTIONS 3中提到OPTIONS哈希的唯一地方!我最初尝试使用选项#3,但我不确定#name是什么。那是另一个问题......(我想知道)。但目前的主要问题是HTML_OPTIONS如何神奇地意味着OPTIONS。

    我担心的是我不确定如何阅读其中一些文档,因此我获得自助的能力有限。请帮忙。

2 个答案:

答案 0 :(得分:1)

我理解你的担忧。我已经检查了为什么它(你给的第一个link_to)通过阅读源来工作。实际上,他们检查传递给remote的html选项中的methodlink_to键,而不是url_options(remote除外)。但是在文档中,他们列出了该字段可以选择的选项,而不是&#39; html_options&#39;,他们将其作为&#39;选项&#39;,这使得看起来这些选项适用于选项和url_options但不适用于html_options。当然看起来很神奇,它的工作方式与你说的那种信息一样。另外&#39; url_options&#39;和&#39;选项&#39;基本相同,我认为这是一个错误的文档。

第二种情况下的呼叫显然与任何可用的签名(参数列表)不匹配。是的,块内的# name实际上意味着应该进入锚标签内的内容。我认为# link content在那里更合适。

所以我认为你并不是(你确定如何阅读文档),就像你说的那样存在一些不一致。 :)

答案 1 :(得分:1)

对于#3,options是URL选项,因此它应该被命名为url_options,如#2。证明:rails source

完全正常的是“ THIS WORKED ”示例有效,并且使用#4而不是#3是完全正常的,因为您提供的第一个参数是session_path(@account.id),这不是哈希值,而#3第一个参数应该是哈希值。

你的“这个没有工作”尝试不起作用也是正常的,因为你在街区之前通过了3个参数,并且没有3个参数的原型,然后是块。

关于一般的Rails文档

我同意文档有时不是很清楚,但通常你可以通过查看示例来猜测缺失的信息。

在您的情况下,您还可以查看生成的HTML。

如果仍然不清楚,唯一的解决方案是查看来源,就像我上面所做的那样。