我有2个型号
class Deal < ActiveRecord::Base
has_many :couponizations, dependent: :destroy
has_many :coupon_codes, through: :couponizations, source: :coupon_code, dependent: :destroy
accepts_nested_attributes_for :coupon_codes, allow_destroy: true
end
Run Code Online (Sandbox Code Playgroud)
和
class CouponCode < ActiveRecord::Base
has_one :couponization, dependent: :destroy
has_one :deal, through: :couponization, source: :deal
Run Code Online (Sandbox Code Playgroud)
这是由多对多关系联系在一起的
class Couponization < ActiveRecord::Base
belongs_to :coupon_code
belongs_to :deal
end
Run Code Online (Sandbox Code Playgroud)
尽管我指定了dependent: :destroy选项,但当我删除交易时,优惠券代码不会被删除.但是,优惠券会成功删除.有没有办法删除对象销毁的关联嵌套记录?
我注意到array.sum并array.inject(:+)产生了不同的结果.这是什么原因?
a = [10, 1.1, 6.16]
a.inject(:+)
# => 17.259999999999998
a.sum
# => 17.26
Run Code Online (Sandbox Code Playgroud) 我有一个城市模型,在城市的展示活动中,我想要在城市中的特定位置附近的酒店.城市有很多地方; 酒店正在使用Geocoder near方法进行搜索.
为了添加订单功能,我一直关注Ryan Bates的截屏视频#228,但这种方法似乎不适用于数组,给出错误undefined method `order' for #< Array:0x007f960d003430>
cities_controller.rb
helper_method :sort_column, :sort_direction
def show
session[:search_radius] = 2 if session[:search_radius].blank?
@city = City.find(params[:id])
@locations = @city.locations
@hotels = []
@locations.each do |location|
unless location.longitude.blank? || location.latitude.blank?
center_point = [location.latitude, location.longitude]
box = Geocoder::Calculations.bounding_box(center_point, session[:search_radius])
thotels = Hotel.near(center_point, session[:search_radius]).within_bounding_box(box)
else
thotels = Hotel.near(center_point, session[:search_radius])
end
@hotels += thotels if thotels
@hotels = @hotels.uniq
end
@hotels = @hotels.order(sort_column + " " + sort_direction).paginate(:page => params[:page], :per_page => 5) …Run Code Online (Sandbox Code Playgroud) 如何从JavaScript侦听器传递变量(id):
Gmaps.map.callback = function() {
...
...
google.maps.event.addListener(marker, 'click', function() {
var id = this.currentMarker;
alert(id);
});
}
}
Run Code Online (Sandbox Code Playgroud)
到ruby-on-rails控制器中的实例变量(@foo)
def create
@foo = ???
...
end
Run Code Online (Sandbox Code Playgroud)
在视图(窗体)中正确创建关系:
<%= form_for user.relationships.build(:followed_id => @foo.id) do |f| %>
<div><%= f.hidden_field :followed_id %></div>
<div class="actions"><%= f.submit "Follow" %></div>
<% end %>
Run Code Online (Sandbox Code Playgroud)
谢谢!
我已经HelloApp用Matlab 编写了一个程序,并使用此处deploytool所述将其打包为.NET程序集.因此我有了库,我将其作为参考包含在Visual Studio 2015中,因此我可以直接从C#运行Matlab方法.要在没有安装Matlab的情况下运行代码,最终用户至少需要安装Matlab Runtime(MCR).helloapp.dll
现在我想为我的C#程序创建一个安装程序.我遇到了Visual Studio 2015安装程序项目,它提供了一些创建程序设置向导的工具.问题是,我不知道如何在安装过程中包含MCR包的下载和设置,以确保用户具有运行程序所需的所有必备条件.
我正在尝试使用基于赛璐珞(gem'celluloid-websocket-client')的Celluloid和Websocket客户端连接到远程websocket .这个客户端对我的主要优点是我可以以类方法的形式而不是块来使用回调.
require 'celluloid/websocket/client'
class WSConnection
include Celluloid
def initialize(url)
@ws_client = Celluloid::WebSocket::Client.new url, Celluloid::Actor.current
end
# When WebSocket is opened, register callbacks
def on_open
puts "Websocket connection opened"
end
# When raw WebSocket message is received
def on_message(msg)
puts "Received message: #{msg}"
end
# When WebSocket is closed
def on_close(code, reason)
puts "WebSocket connection closed: #{code.inspect}, #{reason.inspect}"
end
end
m = WSConnection.new('wss://foo.bar')
while true; sleep; end
Run Code Online (Sandbox Code Playgroud)
预期的产出是
"Websocket connection opened"
Run Code Online (Sandbox Code Playgroud)
但是,我根本没有得到任何输出.可能是什么问题呢?
我在用
gem 'celluloid-websocket-client', '0.0.2'
rails 4.2.1 …Run Code Online (Sandbox Code Playgroud) 我最近将我的应用程序升级到Rails 5,我的下载停止了.当我点击下载按钮时,浏览器挂起,没有任何反应.
class DownloadsController < ApplicationController
def download
send_file(
"#{Rails.root}/public/file.rtf",
filename: "file.rtf",
type: :rtf,
disposition: "attachment"
)
end
end
Run Code Online (Sandbox Code Playgroud)
我有rtf mimetype
Mime::Type.register "text/richtext", :rtf
Run Code Online (Sandbox Code Playgroud)
日志显示一切正常
Sent file /home/deploy/app/releases/20160518213049/public/file.rtf (0.4ms)
I, [2016-05-18T17:34:48.435946 #20202] INFO -- : [d02e8ea3-53da-440d-b3b1-cc6bfd6524dc] Completed 200 OK in 17ms (ActiveRecord: 4.4ms)
Run Code Online (Sandbox Code Playgroud) 我正在使用Ruby on Rails,你能教我如何使用Mandrill发送和接收邮件.
我的问题是当用户输入文本并按提交时,我希望将消息发送到我的邮件.我已阅读文档,但我不明白.
这是我的development.rb,与production.rb相同
ActionMailer::Base.smtp_settings = {
:port => '587',
:address => 'smtp.mandrillapp.com',
:user_name => 'myemail@gmail.com',
:password => 's82SlRM5dPiKL8vjrJfj4w',
:domain => 'heroku.com',
:authentication => :plain
}
ActionMailer::Base.delivery_method = :smtp
Run Code Online (Sandbox Code Playgroud)
user_mailer.rb:
class UserMailer < ActionMailer::Base
default from: "from@example.com"
def welcome
mail(to: "morumotto26@gmail.com", subject: "Welcome", body: "Have a nice day")
end
end
Run Code Online (Sandbox Code Playgroud)
在控制器中:
def index
UserMailer.welcome.deliver
end
Run Code Online (Sandbox Code Playgroud)
我的日志文件如下所示:
Started GET "/users/" for 127.0.0.1 at 2014-01-21 16:14:10 +0700
Processing by UsersController#index as HTML
Sent mail to morumotto26@gmail.com (2008.0ms)
Date: Tue, 21 Jan …Run Code Online (Sandbox Code Playgroud) 我正在使用simple_form,我只想使用分类表创建类别和文章之间的关联.
但我有这个错误:无法批量分配受保护的属性:category_ids.app/controllers/articles_controller.rb:36:在`update'中
articles_controller.rb
def update
@article = Article.find(params[:id])
if @article.update_attributes(params[:article]) ---line with the problem
flash[:success] = "?????? ?????????"
redirect_to @article
else
render :edit
end
end
Run Code Online (Sandbox Code Playgroud)
article.rb
has_many :categorizations
has_many :categories, through: :categorizations
Run Code Online (Sandbox Code Playgroud)
category.rb
has_many :categorizations
has_many :articles, through: :categorizations
Run Code Online (Sandbox Code Playgroud)
categorization.rb
belongs_to :article
belongs_to :category
Run Code Online (Sandbox Code Playgroud)
分类有article_id和category_id字段.
我的_form.html.erb
<%= simple_form_for @article, html: { class: "form-horizontal", multipart: true } do |f| %>
<%= f.error_notification %>
<%= f.input :title %>
<%= f.association :categories %>
<%= f.input :teaser %>
<%= f.input :body %>
<%= f.input …Run Code Online (Sandbox Code Playgroud) ruby-on-rails associations nested-forms ruby-on-rails-3 simple-form
是否可以在控制器和模型之间共享常量?
比如product.rb我有
PRODUCT_TYPES = %w[one two]
Run Code Online (Sandbox Code Playgroud)
我希望PRODUCT_TYPES控制器中也能保持不变.
我正在尝试在 Ruby 和 Python 中为相同的字符串生成 CRC32 校验和并获得不同的结果。
Python
from zlib import crc32
x = "SheetJS"
crc32(x)
#=> -1647298270
Run Code Online (Sandbox Code Playgroud)
节点
var CRC32 = require('crc-32');
var x = "SheetJS";
CRC32.str(x);
#=> -1647298270
Run Code Online (Sandbox Code Playgroud)
红宝石
require 'zlib'
x = "SheetJS"
Zlib::crc32(x)
#=> 2647669026
Run Code Online (Sandbox Code Playgroud) ruby ×3
arrays ×2
actionmailer ×1
activerecord ×1
associations ×1
c# ×1
celluloid ×1
crc ×1
crc32 ×1
download ×1
email ×1
hash ×1
inject ×1
installation ×1
javascript ×1
json ×1
mandrill ×1
matlab ×1
mime-types ×1
nested-forms ×1
python ×1
simple-form ×1
websocket ×1
zlib ×1