小编chi*_*ikh的帖子

JQuery .on()不会将单击事件绑定到动态创建的元素

我有一个文本框,当我按Enter键时动态添加一个元素,另一个文本框在我单击删除按钮时删除该元素.delete方法适用于任何现有元素,但不适用于动态插入的任何元素.

这是代码:

$ ->
    # AJAX to add a new stock
    $("#add-symbol").keypress (e) ->
        if e.which == 13
            url = $(this).data('url')
            name = $(this).val()
            $.ajax
                url: url
                type: "POST"
                data: {
                    user_id: $('#info').data('user-id'),
                    name: name
                }
                success: (response) ->
                    if response.status == 200
                        new_element = '<a class="item" data-path="' + response.path + '" data-stock="' + response.symbol + '">'+ response.symbol + '<i class="icon remove"></i></a>'
                        $('#symbols').append(new_element)
                        $('#add-symbol').val('')
                    else
                     #deal with errors

    # AJAX to delete stocks
    $('.icon.remove').on('click', (e) ->
        console.log('click click')
        $parent = …
Run Code Online (Sandbox Code Playgroud)

javascript jquery ruby-on-rails coffeescript

17
推荐指数
1
解决办法
3万
查看次数

使用Puma在Elastic Beanstalk上部署的Rails应用程序失败 - 每个请求都有502个错误

我刚刚将一个Rails应用程序部署到Elastic Beanstalk,每个请求都给我一个502错误.

这是/var/logs/nginx/error.log的内容

2015/05/20 16:24:25 [warn] 1535#0: conflicting server name "localhost" on 0.0.0.0:80, ignored
2015/05/20 16:27:12 [crit] 1537#0: *20 connect() to unix:///var/run/puma/my_app.sock failed (2: No such file or directory) while connecting to upstream, client: 172.31.51.94, server: _, request: "POST /get HTTP/1.1", upstream: "http://unix:///var/run/puma/my_app.sock:/get", host: "securities-api-prod.elasticbeanstalk.com"
2015/05/20 16:27:17 [crit] 1537#0: *20 connect() to unix:///var/run/puma/my_app.sock failed (2: No such file or directory) while connecting to upstream, client: 172.31.51.94, server: _, request: "POST /get HTTP/1.1", upstream: "http://unix:///var/run/puma/my_app.sock:/get", host: "securities-api-prod.elasticbeanstalk.com"
2015/05/20 16:27:19 [crit] 1537#0: …
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails nginx amazon-ec2 puma amazon-elastic-beanstalk

16
推荐指数
3
解决办法
6127
查看次数

为什么从ASCII-8BIT到UTF-8会出现字符串编码问题"\ xE2"?

我正在尝试从电子邮件下载PDF并将内容写入文件.出于某种原因,我收到此错误:

An Encoding::UndefinedConversionError occurred in attachments#inbound: "\xE2" from ASCII-8BIT to UTF-8 app/controllers/api/attachments_controller.rb:70:in `write'
Run Code Online (Sandbox Code Playgroud)

这是我的代码:

def inbound
    if Rails.env.production? or Rails.env.staging?
      email = Postmark::Mitt.new(request.body.read)
    else
      email = Postmark::Mitt.new(File.binread "#{Rails.root}/app/temp_pdfs/email.json")
    end

    if email.attachments.count == 0
      # notify aidin that we got an inbound email with no attachments
      respond_to do |format|
        format.json { head :no_content }
      end
      return
    end
    attachment = email.attachments.first
    filename = "attachment" + (Time.now.strftime("%Y%m%d%H%M%S")+(rand * 1000000).round.to_s) + ".pdf"
    base_path = "#{Rails.root}/temp_attachments/"
    unless File.directory?(base_path)
      Dir::mkdir(base_path)
    end
    file = File.new base_path + …
Run Code Online (Sandbox Code Playgroud)

ruby string ascii ruby-on-rails utf-8

15
推荐指数
1
解决办法
1万
查看次数

动态添加新元素后,jQuery选择器不会更新

我的选择器是:

$('section#attendance input:last')
Run Code Online (Sandbox Code Playgroud)

但是,我在#attendance部分添加了另一个输入.我希望选择器现在选择该元素(因为它应该如此:last.但是,出于某种原因,它没有,但我不太清楚为什么?

这是我的完整代码:

$('section#attendance input:last').keydown(function(e) {
        if (e.which == 9)
        {
            var $this = $(this);
            var num = parseInt($this.attr('name').match(/\d+(,\d+)?/)[0]) + 1;
            $this.parent().append('<input type="text" name="attendance[' + num +']" value="Name and Position" class="medium" />');
        }
    });
Run Code Online (Sandbox Code Playgroud)

新守则:

    $("section#attendance").on("keydown", "input:last", function(e) {
    if (e.which == 9) {
        var $this = $(this);
        var num = parseInt($this.attr('name').match(/\d+(,\d+)?/)[0]) + 1;
        $this.after('<input type="text" name="attendance[' + num +']" value="Name and Position" class="medium" />');
    }
});
Run Code Online (Sandbox Code Playgroud)

编辑:问题似乎在'输入:最后',因为它只是使用输入它的工作原理.另外,如果我将类'last'添加到最后一个元素,当我使用'input.last'作为选择器时,它就可以工作.

javascript jquery tabs

14
推荐指数
3
解决办法
2万
查看次数

将所有URL重写为index.php,除了'/ Serene/Assets /'

所以我目前在我的.htaccess中将我的所有URL重定向到index.php:

RewriteEngine on
RewriteCond %{DOCUMENT_ROOT} !-f
RewriteRule ^(.*)$ index.php [QSA,L]
Run Code Online (Sandbox Code Playgroud)

但是,我需要网址,例如http://localhost/Portfolio/Serene/Assets/css/style.css不重定向,但我不确定如何?

我真的很感激任何帮助,

干杯.

编辑:或者,我可以告诉htaccess不重定向任何.js,.css,图像文件等,但再次,我不太确定如何做到这一点?谢谢!

EDIT2:我还可以将以.php或路径(例如/ Portfolio /)结尾的任何内容重定向到index.php,我觉得这可能更容易.

EDIT3:成功,最终代码:

RewriteEngine on

RewriteCond %{DOCUMENT_ROOT} !-f
RewriteRule !\.(js|ico|gif|jpg|png|css|html|swf|flv|xml)$ index.php [QSA,L]
Run Code Online (Sandbox Code Playgroud)

php .htaccess

11
推荐指数
2
解决办法
3万
查看次数

如何将哈希打印到Rails视图?

我有类似的东西:

{"a":"1","b":"2","c":"3","asefw":"dfsef"}
Run Code Online (Sandbox Code Playgroud)

我想在视图中打印出来.最好的方法是什么?

我尝试将其解析为JSON对象并使用JSON.stringify,但它似乎搞乱了缩进.

有什么建议?我不介意JavaScript解决方案.

ruby hash json ruby-on-rails pretty-print

10
推荐指数
2
解决办法
2万
查看次数

是否可以在工厂(FactoryGirl)中使用if语句?

我在我的工厂里有两个特性,我希望在创建对象时包含其中一个特征,而不是默认为一个(因此随机选择特征).这是我正在做的事情:

FactoryGirl.define do
  factory :follow_up do
    first_name      { Faker::Name.first_name }
    last_name       { Faker::Name.last_name }
    phone           { Faker::PhoneNumber.cell_phone.gsub(/[^\d]/, '').gsub(/^1/, '2')[0..9] }
    email           { Faker::Internet.email }
    email_preferred true
    consent         false

    if [1, 2].sample == 1
      by_referral
    else
      by_provider
    end

    trait :by_referral do
      association :hospital
      association :referral
      source { FollowUp::REFERRAL}
    end

    trait :by_provider do
      association :provider
      source { FollowUp::PROVIDER }
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

但是,它似乎忽略了if语句并直接转向by_provider trait.谁知道我怎么做?

conditional ruby-on-rails factory-bot

6
推荐指数
1
解决办法
1281
查看次数

以别名执行脚本

我正在尝试别名eclipse来执行eclipse -data $(pwd). 但是,由于某种原因,将此添加到我的 zshrc 中不起作用:

alias eclipse="eclipse -data $(pwd)"

我似乎找不到正确的语法 - 有人可以帮我吗?

eclipse alias zshrc

2
推荐指数
1
解决办法
301
查看次数

Brew更新失败

输出:

? brew update
error: Your local changes to the following files would be overwritten by merge:
    lapack.rb
    whois.rb
Please, commit your changes or stash them before you can merge.
Aborting
Error: Failed to update tap: homebrew/dupes
Already up-to-date.
Run Code Online (Sandbox Code Playgroud)

我在Stack Overflow和Brew问题页面上尝试了很多东西(https://github.com/mxcl/homebrew/wiki/Common-Issues#brew-update-complains-about-untracked-working-tree-files)但仍然没有运气.它看起来有点不同,因为它抱怨自制/欺骗.

homebrew

2
推荐指数
1
解决办法
1959
查看次数

我想为字符串中的所有哈希添加一个哈希值(PHP)

所以在我的字符串中,我有一些哈希的部分.例如,考虑字符串"#Hello,这是一个示例字符串.这是###哈希的另一个例子".

我想用以下代码替换它:"## Hello,这是一个示例字符串,这是####哈希的另一个例子".(请注意,每个实例中的哈希数增加了一个)

但是,我不太清楚如何.我想它涉及正则表达式,我搜索了一下,但我不太清楚该怎么做.

任何人都可以帮助/引导我走正确的道路吗?

干杯

php regex

0
推荐指数
1
解决办法
58
查看次数