小编Ala*_*man的帖子

如何从 Rails 的价格字段中删除“$”和“,”

我将价格字符串保存到我的数据库中的十进制类型列中。

价格是这样的,这"$ 123.99"很好,因为我写了一些代码来删除"$ ".

但是,我忘记了价格可能包含逗号,所以"$ 1,234.99"破坏了我的代码。我怎样才能删除逗号?

这是我删除美元符号和空格的代码:

def price=(price_str)
  write_attribute(:price, price_str.sub("$ ", ""))
  # possible code to remove comma also?
end
Run Code Online (Sandbox Code Playgroud)

ruby ruby-on-rails input

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

如果@ model.save引发错误,请使用Rails

我有一个消息模型。每当我if @message.save在控制器中运行时,都会出现此错误:

ERROR:  null value in column "conversation_id" violates not-null constraint
Run Code Online (Sandbox Code Playgroud)

如果保存失败,我只想重定向。

  def create
    @message = Message.new(message_params)
    if @message.save
      # redirect
    else
      # render new
    end
  end 
Run Code Online (Sandbox Code Playgroud)

我的消息架构

  create_table "messages", force: true do |t|
    t.string   "body",                                    null: false
    t.integer  "conversation_id",                         null: false
    t.datetime "created_at"
    t.datetime "updated_at"
  end
Run Code Online (Sandbox Code Playgroud)

ruby ruby-on-rails-4

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

jQuery函数循环更改背景图像崩溃firefox

我希望背景图像淡出.

我有两个div在彼此之上,背景设置为图像.

在我的jQuery中,我定位一个div,然后将不透明度显示为零,显示另一个.然后我反过来.但是,这是有效的,但现在由于某种原因,这个无限循环崩溃了Firefox.

我该怎么做才能让它不崩溃?

             $(document).ready(function() {
                function change (){
                  $('#back1').animate({opacity:0}, {duration:3000});
                  $('#back1').delay(1000);
                  $('#back1').animate({opacity:1},  {duration:3000});
                  $('#back1').delay(1000);
                  change();

                }

                change();

            });
Run Code Online (Sandbox Code Playgroud)

jquery jquery-animate

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

在html css中为crcular png添加一个大纲

嘿伙计们,我想在这个圆形的png周围添加一个4px的白色圆形边框.

http://oi45.tinypic.com/n6uo8o.jpg

我无法在图片中做到这一点因为我想要稍后悬停效果.请帮忙!谢谢.

html css html5 css3

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

用于登录的Rails小写字段输入

在我的数据库中的rails中,我有一个名为me@gmail.com的用户,但如果我尝试使用ME@gmail.com登录.我不能.显然,我的字段在尝试登录之前并没有变得更低.我知道我应该做...

before_save { |user| user.email = email.downcase }

但据我所知,这在模型中.但我只有一个会话控制器.不是会话模型.那么我应该如何达到同样的效果呢?

我目前的用户模型

class User < ActiveRecord::Base
  attr_accessible :email, :password, :password_confirmation, :name

  attr_accessor :password
  before_save :encrypt_password
  before_save { |user| user.email = email.downcase }
  before_save { |user| user.name = name.downcase }
  before_create { generate_token(:auth_token) }
  before_create { generate_token(:email_token) }
Run Code Online (Sandbox Code Playgroud)

我当前的会话控制器

class SessionsController < ApplicationController
  def new
  end

  def create
    user = User.authenticate(params[:email], params[:password])
      if user
        if user.email_activation_token == true
          if params[:remember_me]
            cookies.permanent[:auth_token] = user.auth_token
          else
            cookies[:auth_token] = user.auth_token
          end
          redirect_to root_url, :notice => "Logged …
Run Code Online (Sandbox Code Playgroud)

ruby login ruby-on-rails input ruby-on-rails-3

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

使 Rails Button_to 链接到带有参数的路径

我正在尝试设置一个转到路径的按钮,但我不知道将路径设置为什么。目前我的设置是这样的:

= button_to "buy" , product_card_path, :method => "get"

但我收到错误。

No route matches {:action=>"card", :controller=>"products"}
Run Code Online (Sandbox Code Playgroud)

这是 rake 路线中的路径:

product_card GET   /products/:product_id/card(.:format)    products#card
Run Code Online (Sandbox Code Playgroud)

如何使 Button_to 转到需要参数的路径?

ruby-on-rails

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

Rails - 制作一年和一个月的下拉菜单

使用月份和年份制作form_for的正确方法是什么.

这是保存到我的用户模型:birth_year和:birth_month.

我试过这两种方式:

= form_for @user do |f|
 = f.label :phone_number
 = f.text_field :phone_number
 %br
 / Method 1
 = f.label :date_of_birth
 = f.date_select :birth_month, :order => [:month] 
 = f.date_select :birth_year, :order => [:year]

 / Method 2
 = select_year(Date.today, field_name: 'birth_year')
 = select_month(Date.today, field_name: 'birth_year')
 %br
 %p.button
   = f.submit
Run Code Online (Sandbox Code Playgroud)

方法1错误:

{"utf8"=>"?", "_method"=>"put", "authenticity_token"=>"+0inJWFRPIrDt=", "user"=>{"phone_number"=>"7185289532", **"birth_month(1i)"=>"2013", "birth_month(3i)"=>"1", "birth_month(2i)"=>"6", "birth_year(2i)"=>"8", "birth_year(3i)"=>"27", "birth_year(1i)"=>"2000"}**, "commit"=>"Update User", "action"=>"update", "controller"=>"users", "id"=>"10"}
Run Code Online (Sandbox Code Playgroud)

方法2不保存给用户:

{"utf8"=>"?", "authenticity_token"=>"+0inJWFRPIr=", "user"=>{"phone_number"=>"7185289532"}, **"date"=>{"birth_year"=>"1945", "birth_month"=>"7"**}, "`commit"=>"Update User", "id"=>"10"}
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails

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

Rails Haml采用内联造型

如何使用haml中的对象url为背景图像指定内联样式?大声笑.这是没有对象的div的工作版本.

.main-image{style: "background-image: url('http://example.com/image.jpg');"}
Run Code Online (Sandbox Code Playgroud)

但是,当我尝试这个:

.main-image{style: "background-image: url(= @photos[0].image.url);"}
Run Code Online (Sandbox Code Playgroud)

当我添加@photos[0].image.url任何想法时它都没有用?

ruby haml ruby-on-rails

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

Rails:获取http响应的值

在rails中我发了一个api的帖子并得到了回复,但我不知道如何获取信息并找到"code"变量的值.

$ response 
#<Net::HTTPOK 200 OK readbody=true>

$ puts response.body
{"success":true,"button":{"code":"dfhdsg7f23hjgfs7be7","type":"buy_now","style":"none","text":"Send to MeBrah","name":"MeBrah","description":"Coins you&#x27;re willing to give me.","custom":"6","callback_url":null,"success_url":null,"cancel_url":null,"info_url":null,"auto_redirect":false,"price":{"cents":40000000,"currency_iso":"BTC"},"variable_price":true,"choose_price":false,"include_address":false,"include_email":false}}

$ response.code
"200"  
Run Code Online (Sandbox Code Playgroud)

我知道代码变量也是相同的"dfhdsg7f23hjgfs7be7"我只是好奇什么命令会返回它的值.

ruby ruby-on-rails http

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

Rails正式参数不能是实例变量吗?

我有一个rails错误,我不知道在哪里解决它.在"产品#show"上我得到了这个

SyntaxError at /products/63  formal argument cannot be an instance variable
Run Code Online (Sandbox Code Playgroud)

我评论了控制器和视图中的所有内容

def show
  # @product = Product.find(params[:id])
end
Run Code Online (Sandbox Code Playgroud)

我的错误gem指向activesupport(3.2.13)lib/active_support/dependencies.rb

  newly_defined_paths = new_constants_in(*parent_paths) do
    result = Kernel.load path     #this is the line with the error
  end
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?谢谢.

ruby ruby-on-rails ruby-on-rails-3

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