<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <title>ahr0u (ahr0u)</title>
    <link>https://ruby-china.org/ahr0u</link>
    <description></description>
    <language>en-us</language>
    <item>
      <title>在 url 中传入关键值，而后在 controller 中对 params 进行判断按需返回数据，这种做法好吗？</title>
      <description>&lt;p&gt;举个栗子吧。&lt;/p&gt;

&lt;p&gt;譬如我有个 posts 的表，其中有个 status 状态字段，其中的值有 draft, new, pending&lt;/p&gt;

&lt;p&gt;在 front.html.erb 中有三个链接，在链接中传入了 status 关键值，&lt;code&gt;&amp;lt;%= link_to "Draft Posts", posts_path(status: draft) %&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;而在 controller 中，通过判断 params 来取出相对应的值&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;when params[:status]
case 'draft'
  Post.where(status: draft)
...
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;虽然我需要的功能实现了，但这样做好吗？怎么做才是最好的呢？&lt;/p&gt;

&lt;p&gt;之所以我觉得有点怪怪的是因为：&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;rails 好像比较注意 url 的美观，我将这些值都以/?status=xxx 的形式传过来貌似不怎么美观&lt;/li&gt;
&lt;li&gt;controller 的部分好像比较臃肿，譬如我很 7 个状态，那就有 7 个 switch 语句的分支&lt;/li&gt;
&lt;/ol&gt;</description>
      <author>ahr0u</author>
      <pubDate>Wed, 08 Oct 2014 23:14:09 +0800</pubDate>
      <link>https://ruby-china.org/topics/21931</link>
      <guid>https://ruby-china.org/topics/21931</guid>
    </item>
    <item>
      <title>osx 下面 brew install mongodb 后，手动启动运行了命令怎么没有任何反应呢</title>
      <description>&lt;p&gt;安装应该是成功了，再次运行 brew install mongodb 被告知已安装，运行 mongod --config /usr/local/etc/mongod.conf什么反应都没有，terminal就定在那里了，明显没有起来，所以无法访问localhost:28017&lt;/p&gt;

&lt;p&gt;运行了 brew info mongodb 结果如下，又安装了 scons 也不行&lt;/p&gt;

&lt;p&gt;==&amp;gt; Dependencies
Build: scons ✘
Optional: boost ✘, openssl &lt;img title=":heavy_check_mark:" alt="✔" src="https://twemoji.ruby-china.com/2/svg/2714.svg" class="twemoji"&gt;&lt;/p&gt;</description>
      <author>ahr0u</author>
      <pubDate>Tue, 07 Oct 2014 08:27:41 +0800</pubDate>
      <link>https://ruby-china.org/topics/21894</link>
      <guid>https://ruby-china.org/topics/21894</guid>
    </item>
    <item>
      <title>请教下关于如何通过 gem 源码学习来提高</title>
      <description>&lt;p&gt;目前我具备了 Ruby 和 Rails 基础，基本的 Rails CRUD app 没有问题，扩充需求的时候通过搜索 gem 也能搞定，接下来就是提高了，我相信提高其中一条路便是学习别人的代码，因此我制定的计划是：&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;先确定一个希望实现的功能&lt;/li&gt;
&lt;li&gt;搜索现有的 gem，找一个代码量最小的&lt;/li&gt;
&lt;li&gt;看别人怎么实现了，然后自己比照着实现&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;但这过程中我发现，即使是很简单的 gem，都用了大量 Ruby 或 Rails 的高阶知识，我现在想请教下大家这些高阶知识应该如何补呢？有没有啥书或者教程可以推荐的？或者什么技巧？&lt;/p&gt;

&lt;p&gt;问题太宽泛了，我举个实际例子吧。&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class ActiveRecord::Base
  def self.record_activities(*actions)
    #association
    options = actions.extract_options!
    if options[:dependent] or options[:association]
      name = options[:association] || :activities
      dependent = options[:dependent] || :nullify
      has_many name, :dependent =&amp;gt; dependent, :as =&amp;gt; :subject, :class_name =&amp;gt; 'Activity'
    end

    #action recording
    actions = [:create, :update] if actions.empty?
    actions.each do |action|
      method = "record_activity_#{action}".to_sym
      define_method method do
        return unless self.class.record_userstamp
        Activity.create(:actor_id =&amp;gt; self.class.stamper_class.stamper, :subject =&amp;gt; self, :action =&amp;gt; action.to_s)
      end
      send("after_#{action}",method) if respond_to?("after_#{action}")
    end
  end
end
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;如上这段代码就这几行&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;我看到&lt;code&gt;extract_options!&lt;/code&gt;就卡住了，Google 之，了解到这原来是 Rails ActiveSupport 中的一个方法，很多 Rails 的代码都有用到它。&lt;/li&gt;
&lt;li&gt;接着，到了&lt;code&gt;:association&lt;/code&gt;这里又不明白了，如果调用这方法传入的参数中有&lt;code&gt;:association&lt;/code&gt;，则将其赋值给 name，然后就赋值&lt;code&gt;:activities&lt;/code&gt;？那调用者得传入个&lt;code&gt;:association&lt;/code&gt;作为 hash key 的参数？&lt;/li&gt;
&lt;li&gt;而这&lt;code&gt;:activities&lt;/code&gt;又是哪里来的呢？之前没有看到过呀，要怎么找到它是在哪里定义的呢？同理&lt;code&gt;:nullify&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;跳到下半段，&lt;code&gt;define_method&lt;/code&gt;貌似是在动态的创建方法？这是元编程的东西吧？以及&lt;code&gt;send&lt;/code&gt;和&lt;code&gt;respond_to?&lt;/code&gt; &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;希望这个例子都让大家了解到我的问题所在，感谢你的指点 :)&lt;/p&gt;</description>
      <author>ahr0u</author>
      <pubDate>Sat, 20 Sep 2014 11:27:39 +0800</pubDate>
      <link>https://ruby-china.org/topics/21627</link>
      <guid>https://ruby-china.org/topics/21627</guid>
    </item>
    <item>
      <title>每月 ¥100 左右的预算在国内什么平台部署最合适呢</title>
      <description>&lt;p&gt;用来跑公司内部应用的（可能连域名都不用，直接 IP 访问 :），所以要求不高只求稳定靠谱省心（不考虑 heroku 因为访问速度问题，而且最奇葩的是，我们公司网络好像被电信注入了还是啥的，访问 herokuapp 还有弹窗广告...）&lt;/p&gt;</description>
      <author>ahr0u</author>
      <pubDate>Mon, 18 Aug 2014 11:10:04 +0800</pubDate>
      <link>https://ruby-china.org/topics/21080</link>
      <guid>https://ruby-china.org/topics/21080</guid>
    </item>
  </channel>
</rss>
