Tag: 的Ruby on 轨道

优雅的方式只传递rails redirect_to中的非null参数

我们如何在rails redirect_to方法中仅传递非null参数? 我知道我们可以通过以下方式将参数传递给redirect_to: redirect_to :action => “action1”, :foo => bar 当变量’bar’可能为零或空时,是否有任何优雅/更好的方法来传递它? 现在,我在执行redirect_to之前检查bar是否为空。 但我觉得这可以用更优雅的方式完成。 if bar.blank? redirect_to :action => “action1” else redirect_to :action => “action1”, :foo => bar end

RoR:语法错误,意外的tSTRING_BEG,期待’)’

我在rails视图中收到以下错误: app/views/welcome/index.html.haml:55: syntax error, unexpected tSTRING_BEG, expecting ‘)’ …ut.attributes({}, nil, :value=”#{num}”)}>#{_hamlout.adjust_t… … ^ 这是造成它的两条线(分别为54和55) =(1..52).each do |num| %option{:value=”#{num}”} #{num.to_s} weeks 我正在尝试将“number”与“周”连接到+ string运算符。 但我的方法显然是错误的。

如何获取我的js.erb文件?

我的js.erb文件不会出现在Rails 3.1.2中,这让我很困惑。 我将它包含在app/views/items 。 我有这个: respond_to do |format| format.html { render :action => :index } format.js {render :content_type => ‘text/javascript’ } end 并且它的名字正确。 我尝试了不同的名称组合,但我甚至不理解如何包含js.erb,因为它不在我的源代码中。 我发出alert(‘hello’); 在js.erb ,它不会运行。

使用Rails嵌套资源

我是rails的新手,我开始使用嵌套资源。 我一直在阅读这个http://guides.rubyonrails.org/routing.html#nested-resources,所以我创建了两个模型,一个产品和一个发件人。 产品有很多发件人。 我的发件人模型是这样的: class Sender < ActiveRecord::Base attr_accessible :product_id, :email, :name belongs_to :product end 我的产品就是这个 class Product < ActiveRecord::Base attr_accessible :name, :price #Relationships ! has_many :senders, dependent: :destroy end 在我的routes.rb中: resources :products do resources :senders end 根据http://guides.rubyonrails.org/routing.html#nested-resources ,现在rake路线为我提供了所需的所有正确路线 所以当我输入url时 http://localhost:3000/products/1/senders/new 所以我为我的产品创建了一个id = 1的新发件人我得到了这个: NoMethodError in Senders#new undefined method `senders_path’ for #<#:0x00000003e6f408> 为什么我会得到这个未定义的方法,因为它应该为我的产品的发送者提供new.html.erb页面?

制图员插件渲染奇怪的地图

这是描述问题的图像: 所以我使用https://github.com/parolkar/cartographer (在这个阶段只能作为插件使用),并且我正在获得如上所示的不寻常的地图渲染。 关于图标位置等,一切看起来都很好。但是当我点击它们时,我会得到时髦的信息框(包含正确的信息)。 这是我用来生成地图和信息框的代码: @map = Cartographer::Gmap.new( ‘map’) @map.zoom = :bound @icon = Cartographer::Gicon.new() @map.icons < “Business#{@count}”, :marker_type => “Building”, :position => [@lat[@count], @long[@count]], :info_window_url => “/bio/#{@branch_id[@count]}”, :icon => @icon) @map.markers << markername @count += 1 end 来自控制器: def bio @branch = Branch.find(params[:id]) render :layout => false end 和生物视图文件

如何让我的OpenidController在Ruby on Rails中工作?

我收到这个错误: 未初始化的常量OpenidsController 我无法弄清楚为什么。 我正在遵循本指南: http : //www.danwebb.net/2007/2/27/the-no-shit-guide-to-supporting-openid-in-your-applications 我使用以下命令生成控制器: script/generate controller Openid new create complete 我已经在我的路线文件中添加了以下行,正如指南所说: map.resource :openid, :member => { :complete => :get } 有任何想法吗? 我是RoR的新手,所以希望这对其他人来说很容易。

在created_at中的Rails和时区

ruby-1.9.2-p0 > SalesData.last => # ruby-1.9.2-p0 > SalesData.last.created_at => Tue, 05 Apr 2011 20:53:21 CEST +02:00 application.rb中: config.time_zone = ‘Copenhagen’ 我不明白 – 有人吗?

如何将country_select用作select_tag?

在form_for中使用country_select非常简单。 但现在,我想用它作为按国家过滤的方式。 你能指出我正确的方向吗?

订阅不是在自定义条带表单上创建,而是在Stripe上创建订阅

目前我的代码没有在数据库中创建实际的订阅项,但它正在Stripe上创建订阅。 我已经检查了日志,在订阅表单完成时我看不到任何创建的项目。 我玩了一下并将stripe代码从before_create更改为after_create,这似乎有效,但这没有意义,因为我们只能在用户通过Stripe订阅时给予用户订阅。 有任何想法吗? 谢谢! subscriptions_controller.rb class SubscriptionsController “Thank you for subscribing!” else render :new end end def show @subscription = Subscription.find(params[:id]) end def subscription_params params.require(:subscription).permit(:stripe_card_token) end end subscription.rb class Subscription stripe_card_token, :description => “name”, :plan => 121, :email => “email”) self.stripe_customer_id = customer.id self.plan = 121 end end subscriptions.js.coffee # Place all the behaviors […]

你如何确保has_many总是“有一个最小”?

假设我正在编写代码来建模公交车站。 每个公交fleet总是至少有bus 。 我想弄清楚在模型中强制执行此最小数字的最佳方法。 除非舰队本身被摧毁,否则舰队中的最后一辆公共汽车永远不会被毁坏。 一种方法是捕获bus对象上的关系。 class Bus … before_destroy :check_minimum_busses_on_parent end 然而,这普遍忽视了单一责任原则。 公共汽车正在处理不是问题的车队问题。 我可以使用before_destroy方法创建一个bus_observer 。 现在一切都是单一的责任,感觉就像我们有更多的物体在不必要的时候发出嘎嘎声。 class BusObserver < ActiveRecord::Observer def before_destroy bus false if bus.fleet.busses.count <= 1 end end 这肯定会起到作用,但我对于它真正属于Fleet类的脱钩并不是百分之百。 仍然允许舰队摧毁它的最后一class车 为了允许父fleet对象本身可能被摧毁并想要摆脱它的最后一辆公共汽车的可能性,我最终还需要一些交叉通信,所以公共汽车可以弄清楚舰队是否也得到了印章: class BusObserver < ActiveRecord::Observer def before_destroy bus if bus.fleet.busses.count <= 1 false unless bus.fleet.currently_being_destroyed? end end end 这显然进入了纯粹丑陋的境界。 可以将它包装成方法.bus_can_be_destroyed? 坐在舰队对象上,但这并不是更好。 得到我所追求的最简洁的方法是什么?