Tag: ruby on rails 3

Rails:通过form_for check_box为HABTM关系分配多个参数

我是第一个自己项目的铁杆菜鸟。 我使用has_and_belongs_to_many关系链接了两个模型:Wine和Shop。 为了简单起见,葡萄酒可以在不同的商店出售,特定的商店可以出售许多不同的葡萄酒。 以下是模型: class Shop < ActiveRecord::Base has_and_belongs_to_many :wines end class Wine < ActiveRecord::Base has_and_belongs_to_many :shops end 我的目标是创建一个表单来创建Wine的实例,包括可以购买葡萄酒的商店。 这是我的wines_controller: def new @wine = wine.new @shops = Shop.all respond_to do |format| format.html # new.html.erb format.json { render json: @wine } end end def create @wine = Wine.new(params[:wine]) params[:shops].each do |id| @wine.shops << Shop.find(id) end end […]

如何在下拉菜单中将每个选项与关联simple_form调用相关联?

我使用simple_form插件有这个表单: true) do |f| %> @video.comment_titles, :label => “Comment Title:”, :include_blank => false %> false, :placeholder => “Post a comment.” %> “Post” %> 这将创建一个包含此行的下拉列表: @video.comment_titles, :label => “Comment Title:”, :include_blank => false %> 我的问题是你如何修改这段代码,以便每个下拉项目都是每个comment_title的个人展示视图的链接? UPDATE 这是从第一个答案的代码生成的html: #<CommentTitle:0x10353b890>”>#<CommentTitle:0x10353b890> #<CommentTitle:0x1035296e0>”>#<CommentTitle:0x1035296e0> #<CommentTitle:0x1035295a0>”>#<CommentTitle:0x1035295a0>

有没有办法在rails中停止此默认插入

好的,我有这个数据结构 class User :positions has_many :positions class Company :positions class Position < ActiveRecord::Base belongs_to :company belongs_to :user attr_accessible :company_id, :user_id, :regular_user end 和我的数据库结构 create_table “positions”, :force => true do |t| t.integer “company_id” t.integer “user_id” t.datetime “created_at”, :null => false t.datetime “updated_at”, :null => false t.boolean “regular_user”, :default => true end 如果我向用户公司添加另一家公司,则regular_user标志始终设置为true 1.9.3-p125 :013 > @user.companies […]

Rails 3 ActiveRecord每个关联模型的模型总和

我有2个型号 Category – id – name 和 Transaction – id – category_id – amount 我想找到每个类别的所有交易的总和。 我知道我可以获得一个caterogies列表,然后使用category_id得到所有交易的总和,但它会做20多个查询。 有没有办法在一个查询中完成所有操作? 编辑:我想最终得到[[category1,sum],[category2,sum]]的列表。

在带有HTTParty的rails应用程序中使用Postmates API

我一直在努力将Postmates与我的电子商务rails应用程序集成,以便按需交付。 我已经为测试目的构建了一个控制器和视图,并在我的路由文件中构建它(在我的开发环境中构建)。 作为参考,这里是Postmates的文档: docs ,这是一个技术博客: 博客 路线文件: resources :postmates do member do post ‘get_delivery’ end end 控制器: require ‘httparty’ require ‘json’ class PostmatesController { :dropoff_address => “205 E 95th Street, New York, NY 10128”, :pickup_address => “619 W 54th St, New York, NY 10019” }.to_json, :basic_auth => { :username => api_key }, :headers => { […]

在能够使用Capistrano进行部署之前,是否需要在我的服务器上手动安装Bundler?

标题基本概括了所有内容。 我刚刚使用Ruby 1.9.2设置了我的Web服务器。 这是一个干净的安装 – 尚未安装任何gem。 另外,我刚刚为我的Rails应用程序配置了Capistrano。 当我运行cap deploy ,它失败并显示以下错误消息: *** [err :: bogusip.com] bash: bundle: command not found 鉴于我所做的就是将Ruby 1.9.2安装到我的服务器上,此时Bundler不存在。 Capistrano是应该为我安装Bundler gem还是我自己应该这样做?

before_filter set_locale除了控制器

我的routes.rb MyApp::Application.routes.draw do scope ‘(:locale)’ do #all resources here end namespace :blog do resources :posts, :only => [:index, :show] end end 我的application_controller.rb class ApplicationController < ActionController::Base # # before_filter :set_locale private def default_url_options(options = {}) {locale: I18n.locale} end def set_locale #code for detect locale here end # # end scope ‘(:locale)’内的所有资源都正常工作。 但是我不想使用带有namespace :blog语言环境namespace :blog ,当我尝试点击博客链接时,我可以看到这个urlhttp://localhost:3000/blog/posts?locale=en […]

表单中的Button_to标记关闭表单

我目前正在使用ruby on rails 3中的表单。因此,在我的项目表单中,我有一个按钮,可以在此项目中签署用户。 然而,奇怪的是, button_to标签关闭了我的整个表单,我不能再使用我的提交按钮了。 所以我尝试了很多,但我不明白为什么会这样。 所以这是我的forms: ‘control-label’, style: ‘margin-bottom: 20px’ %> ‘btn btn-primary’ %> t(“helpers.links.cancel”)), projects_path, :class => ‘btn btn-default’ %> 在我看来,这个代码看起来很好,因为所有标签都完全关闭。 但是,我认为使用button_to时可能会有一些魔力,所以也许有人知道更好的方法来做我想做的事情。 谢谢!

Rails添加两个具有活动记录结果的范围

我正在使用act-as-taggable-on gem 我使用单个字段按tag_name和user_name进行搜索 User.rb class User “INNER JOIN taggings ON taggings.taggable_id = user.id\ INNER JOIN tags ON tags.id = taggings.tag_id AND taggings.taggable_type = ‘User'”, :conditions => [“tags.name = ?”, tag], :order => ‘id ASC’ } } def self.search(search) if search where(‘name LIKE ?’, “%#{search}%”) + tagged_with(search) else scoped end end end 但我有分页问题,​​而得到这个数组,我在config / initializer / […]

PG ::错误:错误:关系schema_migrations的权限被拒绝

不知道为什么我遇到这个错误。 我正确设置了postgresql,只是运行了一个迁移,然后rake db:migrate,我得到了标题错误。 这是我的: database.yml的 development: adapter: postgresql encoding: unicode database: my_blog_development pool: 5 username: my_blog password: test: adapter: postgresql encoding: unicode database: my_blog_test pool: 5 username: my_blog password: production: adapter: postgresql encoding: unicode database: my_blog_production pool: 5 username: my_blog password: 完整错误: [my_blog]$rake db:migrate rake aborted! PG::Error: ERROR: permission denied for relation schema_migrations : SELECT […]