Rails 4多对多关联不起作用

Ruby on rails新手在这里。 试图创建一个入门博客应用程序,并在我的模型之间的多对多关联中遇到问题。 我有2个模型 – post,类别,彼此之间有多对多关联。 我的问题:当我创建一个新post时,Post会被保存,但是类别后关联不会保存在categories_posts表中。 我的代码如下。 感谢您对此的投入。 post.rb class Post < ActiveRecord::Base validates_presence_of :title, :body, :publish_date belongs_to :user has_and_belongs_to_many :categories end category.rb class Category < ActiveRecord::Base validates_presence_of :name has_and_belongs_to_many :posts end categories_posts.rb class CategoriesPosts < ActiveRecord::Base end 迁移 – create_posts.rb class CreatePosts < ActiveRecord::Migration def change create_table :posts do |t| t.string :title […]

使用’file’s chef-solo资源更新文件

我正在尝试使用chef-solo安装java。 问题是在/etc/profile文件中设置JAVA_HOME和PATH变量。 我尝试使用厨师提供的’file’资源。 这是我的一些代码: java_home = “export JAVA_HOME=/usr/lib/java/jdk1.7.0_05” path = “export PATH=$PATH:/usr/lib/java/jdk1.7.0_05/bin” execute “make_dir” do cwd “/usr/lib/” user “root” command “mkdir java” end execute “copy” do cwd “/usr/lib/java” user “root” command “cp -r /home/user/Downloads/jdk1* /usr/lib/java” end file “/etc/profile” do owner “root” group “root” action :touch content JAVA_HOME content PATH end 但问题是content命令会覆盖文件的所有内容,有没有办法在使用chef-solo资源时更新文件。 谢谢! 更新:我已经找到了一些来自chef-recipe代码,但我不确定它究竟做了什么,代码是.. ruby_block “set-env-java-home” […]

如何在ruby中运行后台线程?

我是ruby的新手,并认为重建一个我用C#编写的简单聊天程序是个好主意。 我正在使用Ruby 2.0.0 MRI(Matz的Ruby实现)。 问题是我想在服务器运行时为简单的服务器命令提供I / O. 这是从示例中获取的服务器。 我添加了使用gets()获取输入的命令方法。 我希望这个方法在后台运行作为一个线程,但该线程阻止了另一个线程。 require ‘socket’ # Get sockets from stdlib server = TCPServer.open(2000) # Socket to listen on port 2000 def commands x = 1 while x == 1 exitProgram = gets.chomp if exitProgram == “exit” || exitProgram == “Exit” x = 2 abort(“Exiting the program.”) end end […]

如何用Sunspot突出显示单词?

我想在文本中突出显示已找到的单词,例如,如此处所示。 据我所知,我必须遵循以下步骤: 1)在我的模型中,我必须在我想要突出显示的字段中添加:stored => true选项: searchable do text :title, :stored => true text :description end 2)在我的控制器中,我必须声明我想要突出显示的字段: def search @search = Article.search do keywords params[:search] do highlight :title end end end 3)在视图中我不知道该怎么做,我试过这个: – @search.each_hit_with_result do |hit, result| %p= link_to raw(hit_title(hit)), article_path(result) 这是做方法hit_title : def hit_title(hit) if highlight = hit.highlight(:title) highlight.format { |word| “#{word}” } else h(hit.result.title) […]

Ruby + Cucumber:如何在代码中执行黄瓜?

我想从Ruby代码中执行Cucumberfunction。 通常,与gem一起安装的cucumber二进制文件在命令行上执行,并指定一个或多个function。 但是,我想定义创建动态function执行流程的逻辑。 换句话说,程序可以确定应该执行哪些function。 是否可以使用Ruby代码中的指定function文件来实例化Cucumber而不是命令行?

使用Rails构建Facebook应用程序的当前资源是什么?

我正在寻找使用Rails构建一个基本的Facebook应用程序。 您推荐的资源(书籍播客,截屏video,博客文章等)是什么? (每个post一个答案,并且投票而不是重复)。

Regexp:如何获得每一组MatchData?

我有以下Regexp: regexp=/(\w+) \s* : \s* (\w+) \s+ (.*) \s* ;?/ix 而我正试图获取捕获: names, direction, type = iotext.match(regexp).captures 这适用于单个“x:in integer;” , 但我怎么能在我的文件中获取所有其他匹配数据组: “x : in integer; y : in logic; z : in float;”

我怎么知道BigDecimal是否无法解析?

我正在从csv导入数据,我需要将一些值转换为BigDecimal,如果无法解析则会引发错误。 从测试开始,BigDecimal(“无效数字”)返回一个0的BigDecimal。这没关系,但有点凌乱,除了有效值为0 … Float(“无效数字”)的行为不同并抛出exception…… 我目前的解决方案是: class String def to_bd begin Float(self) rescue raise “Unable to parse: #{self}” end BigDecimal(self) end end 我完全错过了什么吗?

创建一个使用Sinatra的路由只接受某种内容类型

我正在尝试创建一个使用Sinatra的路由,它只接受带有Content-type: application/json POST Content-type: application/json但没有成功。 我的方法如下: post ‘/dogs’, :provides => :json do # returns here a json response end 使用curl进行测试,我已经看到:provides => :json配置路由以使用Content-Type: application/json进行响应。 这是正确的,因为我还希望用POST请求的JSON消息进行响应,但我真的需要这条路由只响应具有Content-Type: application/json POST请求,而不是响应其他人(例如Content-Type: application/xml )。 在Sinatra中是否有任何方法限制路由只接受具有特定Content-Type请求?

当查询使用包含时,Rails如何处理has_many?

如果我有一个包含许多post的用户模型,Rails会在以下场景中对数据库执行多少次查询? class User has_many :posts # this is the main method in question… def has_posted? posts.any? {|p| p.posted? } end end # has an attr “posted” which is a boolean class Post belongs_to :user end # some controller users = User.includes(:posts).all # in the view 当我循环遍历has_posted?的返回post时,我在初始查询中使用的事实是否有任何魔力has_posted? 防止每个用户对posts表进行多次查找的方法?