• 这不是老早就有了的吗

  • 连个链接都懒得编辑,怀疑楼主的诚意。短短不到三行字,倒是罗列了不少名词。。。

  • 赞!现在好像也就上海那边 Elixir 有 meetup 吧?Tony 哥啥时候来珠三角地区布道?

  • group.members << "来自广州到优秀的 React 工程师"

    错别字

  • 反正我已经点了赞!

  • 为什么要直接绑定到每个 <li> 元素上?这个可是会有性能消耗的。用 #8 楼 @Rei 的方案比较好。

    $("ul.xxx").on("click", "li", function(event){
      // do something
    });
    
  • 融云服务器端 API Ruby SDK at November 03, 2016

    #8 楼 @ruby_sky 心领心领,年底结算就好。

  • 融云服务器端 API Ruby SDK at November 03, 2016

    #6 楼 @akirapanda 代码比文档好的典范,哈哈

  • 融云服务器端 API Ruby SDK at November 03, 2016

    #1 楼 @helperhaps 跟之前的极光推送的那个是异曲同工,不过至少极光那个能跑起来。

  • 对于外包可能真是这样,客户会想,如果这个项目做完,后边又很难找到相同技术栈的人来维护,那他干嘛不一开始就找一个多人用的技术栈呢?

  • 小波真是走遍祖国大江南北啊!

  • #3 楼 @leondu 支持!特别同意第二点。

  • #1 楼 @nong 你行你上。

    (PS: 有时候还真希望社区提供 💔 踩 的功能 )

  • It's time to talk! at October 16, 2016

    @xifengzhu 话说拍照就好好拍照啊,摸着别人腿是几个意思

  • It's time to talk! at October 12, 2016

    🔜 沙发!!! 🛏

  • #10 楼 @daemon 那就不是 or 的问题了,or 是同表,表示的是多种条件的组合。

  • #6 楼 @daemon 用 Arel table 可以做到

    users = User.arel_table
    User.where(users[:foreign_field_id].in([1, 2, 3]).or(users[:foreign_field2_id].in([1,2,3]))).to_sql
    # => "SELECT `users`.* FROM `users` WHERE (`users`.`foreign_field_id` IN (1, 2, 3) OR `users`.`foreign_field2_id` IN (1, 2, 3))"
    
  • locale 这个变量来自何方? at September 30, 2016

    #6 楼 @cisolarix 在 controller action 里边放了个断点 binding.pry

  • locale 这个变量来自何方? at September 30, 2016

    #2 楼 @dimos locale 是 actionview 里边的代理方法:

    [1] pry(#<HomeController>)> show-source locale
    
    From: /Users/martin/.rvm/gems/ruby-2.3.1/gems/actionview-5.0.0/lib/action_view/view_paths.rb @ line 11:
    Owner: ActionView::ViewPaths
    Visibility: public
    Number of lines: 2
    
    delegate :template_exists?, :any_templates?, :view_paths, :formats, :formats=,
             :locale, :locale=, :to => :lookup_context
    

    lookup_context 方法定义如下:

    def lookup_context
      @_lookup_context ||=
        ActionView::LookupContext.new(self.class._view_paths, details_for_lookup, _prefixes)
    end
    

    ActionView::LookupContext 中有相关的实现代码:

    module ActionView
      class LookupContext
        def locale
          @details[:locale].first
        end
    
        # Overload locale= to also set the I18n.locale. If the current I18n.config object responds
        # to original_config, it means that it has a copy of the original I18n configuration and it's
        # acting as proxy, which we need to skip.
        def locale=(value)
          if value
            config = I18n.config.respond_to?(:original_config) ? I18n.config.original_config : I18n.config
            config.locale = value
          end
    
          super(default_locale)
        end
      end
    end
    

    天色已晚,我就不深入研究了,反正路是这条路,还可以继续看 LookupContext 里的 self.register_detail 方法定义以及调用。

    另外,Ruby 全局变量名是用 $ 开头的,如果遇到一个看起来像变量但又有全局特性的“变量”的话,基本可以判断其实它是一个方法了。

    另外,像这种生僻方法,还是减少使用比较好,增加同一项目共同维护者的负担。还是用 I18n.locale 就好了。

  • erb 是由 ruby 在服务器端渲染的时候才能执行的代码,而 js 代码是已经在浏览器端渲染执行的了,两个渲染时机是完全不同的。楼主还是没有消化我昨晚发的回复,同一个问题,没必要开那么多帖。

  • 简单重构可以是以下这样的,我假设你渲染的页面是 app/views/players.html.erb

    <!-- app/views/players.html.erb -->
    <%= form_for(:player) do |f| %>
      <%= f.label :sum_player, "游戏人数"%>
      <%= f.number_field :sum_player, in: 1..12, class: 'form-control',id:"sumofplayer" %>
    <% end %>
    
    <button type="button" id="myButton"  class="btn btn-primary" >分配角色</button>
    
    <!-- 这个地方的代码可以去掉
    <p>
      <%= link_to "A post", playerrole_path, remote: true ,method: :post,class: "btn btn-primary"%>
    </p>
    -->
    
    <div id="role-container">
      <div id="player_role"></div>
    
      <!-- 这个按钮固定就好了,没必要像原来一样动态添加 -->
      <button type="button" id="myButton"  class="btn btn-primary" >确定角色</button>
    </div>
    
    <script>
      // 全局变量命名空间
      App ||= {};
      // 这里预先加载角色相关信息,以供后边脚本使用
      App.playerRoles = <%== render 'games/player_role', :player => [1,'bandman',3,4]  %>;
    </script>
    

    而跟这个页面配套的脚本应该是同时下载的,里边的内容改为:

    $ ->
      $playerCountInput = $('#sumofplayer')
    
      if $playerCountInput.length
        badManNumber = 2
        $playerRole = $('#player_role')
    
        gRandomArr = ( arr, num_of_badman ) ->
          for i in [0...num_of_badman]
            # 直接用随机下标就可以了,原来的方式还得整个数组重新排序,没必要
            randomIndex = parseInt(Math.random() * arr.length)
            arr[randomIndex] = 'badman'
    
        make_role_for_players = (content, num_of_badman) ->
          arr = [] # arr 的初始化放在函数里就好了,没必要放外边
          $playerRole.empty();    # 用 $.fn.empty() 方法更语义化,比起 $.fn.html("") 更舒服
    
          if content
            for num in [0...content]
              arr.push( num )
            gRandomArr(arr, num_of_badman)
            for x,index in arr
              $playerRole.append('<p>' + index + (if arr[index] == 'badman' then '内奸' else '') + '</p>');
    
        $playerCountInput.on "change", (e)->
          make_role_for_players $playerCountInput.val(), badManNumber
    

    整个过程就没有任何需要往后端发送请求的必要,以上代码仅为示例,我没法运行,你自己看看吧。还不明白的话,就只能奉劝楼主老老实实重新认真学习基础知识了。

  • #6 楼 @zix 没看出来你这个问题跟 erb 有半毛钱关系,因为你所有逻辑都不需要经过后端处理或者渲染啊,你提交了一个表单,但是你的 ruby 代码根本不需要做任何处理,这个表单对于后端来说就是废的,最后你只是返回一个 js 文件而已,这个文件里边除了 render 'games/player_role' 之外,其他全是纯 js 代码,这个文件应该是页面渲染的时候就一起给浏览器端的,而不是单独用一个 ajax 来请求啊。 这本来是一个简单问题,所有逻辑浏览器端 js 处理就完事了,结果你非得去后端绕一圈再回来,简单问题复杂化啊。

  • #4 楼 @zix 没太明白,贴下代码吧。好多概念乱蹿,你后端不是用的 Ruby,用的 Node.js?为什么“后端通过一个 js 程序进行计算”?

  • #2 楼 @zix 没有。这个问题本身不复杂也不深奥,是你没有理清楚一个请求的生命历程而已,混淆了其中的区别而已。感觉楼主需要好好阅读一下 《一次完整的 HTTP 事务是怎样一个过程?》这样的文章。

  • 楼主没有理解渲染的机制。erb 是服务器端渲染的模板,渲染后的 HTML 下载到浏览器,浏览器才开始渲染 HTML 跟解析 js。你这种问题是倒过来的流程了,怎么可能实现呢?或者你用 ajax 的方式,先得到一个 javascript 数组,然后通过 ajax 请求将数组作为参数提交给后端,后端按照你给的参数渲染 HTML 片段,你再用 js 将这个片段替换到你想替换的位置。

  • #10 楼 @lgn21st 不是的,室外更稳定,室外用的是 GPS 定位,室内用视觉定位。

  • 无敌,无敌是多么寂寞!

  • @lgn21st 我自己重新看了一遍,感觉图片都没有修好,我先取消分享了,这段时间慢慢认真修一遍再重新发出来吧。

  • #2 楼 @michael_roshen 贴 nginx 配置吧