Tag: ruby

如何从另一个.rb文件中访问模块内部和类内部的Ruby方法

我想知道如何从另一个.rb文件访问此模块中的方法 module Decisioning module Decision class OfferProxy < FinanceApplication::Offer def my_method "some value" end end end end 那么我如何从另一个.rb文件访问my_method? maby之类的东西 include :: Decisioning :: Decision :: OfferProxy 我可以再用吗 my_method

在另一个目录中显示部分和索引

我有两个脚手架 用户 userscuentas 我想在文件app/views/users/_layout_form.html.erb显示文件app/views/userscuentas/index.html.erb和app/views/userscuentas/new.html.erb 。 我在file app/views/users/_layout_users.html.erb这样做了: “/userscuentas/index.html.erb” %> 通常我们生成脚手架时,索引文件是默认的,那么这种方式是模型: 应用程序/模型/ user.rb has_many :userscuentas, :dependent => :destroy 应用程序/模型/ userscuentas.rb belongs_to :user 和routes.rb是这样的: resources :users do resources :userscuentas end 我收到了这个错误: NoMethodError at /users/1/edit undefined method `each’ for nil:NilClass 文件控制器userscuentas_controller.rb具有: @userscuentas = @user.userscuentas.all 我不知道该怎么办。

link_to调用的函数即使在硬编码时也不起作用

出于某种原因,在我的函数make_feed_preference , @user.feed_preference永远不会改变,即使我将其硬编码为@user.feed_preference = “foobar”但看起来redirect_to ‘posts’工作正常。 错误是即使在按下link_to之后, feed_preference总是nil ,因此( if feed_preference == nil )始终为true 。 我仍然不知道是什么导致了这一点。 make_feed_preference存储在make_feed_preference中。 它的作用是显示新闻源,但是根据偏好值排序(例如,“时间”,“trending_value”,“粉丝”) :make_feed_preference, :id => current_user.id, :preference => “trending value”, :controller => “users”}, :method => :post %> def make_feed_preference @user = User.find(params[:id]) @user.feed_preference = params[:preference] @user.save redirect_to ‘/posts’ end 注意:我通过将新类附加到用户来修复它。

Rails Neo4j如何在现有数据库中添加新字段

class Invitation include Neo4j::ActiveNode property :email end 这是Neo node Invitation节点我可以在图形数据库中看到它。 – 如果我在其中添加一些新字段,对于现有节点,它将不会反映在图形数据库中 – 如果我创建一个新节点,我可以在图形数据库中看到它 所以问题是我是否必须添加一个字段 property :valid, type: Boolean, default: true 我如何添加这个以便我可以在图数据库中的现有节点中看到它,就像我们在活动记录迁移中一样 我在Node中添加了字段 property :vish, type: Boolean, default: true 所以当查询 Invitation.where(vish: true).count ==>结果0 如果我添加新节点Invitation.create(email:’urvishh@gmail.com’) 然后运行查询 Invitation.where(vish: true).count ==>结果1 这是我得到的确切问题

在从Beginning Ruby书的第12章中尝试一些代码时,如何摆脱这个RunTimeError?

对不起,如果这是一个总的新手问题,但我不知道如何解决这个问题。 我在尝试运行以下代码时遇到这些错误: bot.rb:58:in `rescue in initialize’: Can’t load bot data (RunTimeError) bot.rb:55:in `initialize’ basic_client.rb:3:in `new’ basic_client.rb:3:in `’ 这是bot.rb的源代码,错误似乎出现在“@data = YAML.load(File.open(options [:data_file])。read)”部分。 # A basic implementation of a chatterbot class Bot attr_reader :name # Initializes the bot object, loads in the external YAML data # file and sets the bot’s name. Raises an exception if # […]

如何使用elasticsearch进行排序?

我正在尝试制作排序desc和asc 。 我正在使用rails,tire gem和elasticsearch。 我试图弄清楚我可以在URL中发送什么类型的参数 所以我在搜索块中定义它正在对结果desc顺序进行排序。 sort { by :price, “desc”} 当用户在: new-york中搜索公寓时,结果是排序顺序。 搜索查询/ URL如下所示: http://localhost:3000/apartmens?utf8&query=newyork 为什么我不能在url中添加sort-params,如下所示: http://localhost:3000/apartmens?utf8&query=newyork&sort=asc

使用facebook登录集成键的spree_social gem的初始化程序

我有一个基于狂欢的应用程序,应该与Facebook集成。 我这样做是通过spree_social gem工作正常,但我需要一些配置文件,所以我不必每次点击管理面板。 我添加了ne初始化程序: /config/initializers/devise.rb require ‘omniauth-facebook’ Devise.setup do |config| config.omniauth :facebook, KEY, SECRET end 但这没有任何作用。 我在网上尝试了所有可能的教程和示例,即https://github.com/plataformatec/devise/wiki/OmniAuth:-Overview或http://sleekd.com/tutorials/setting-up-omniauth-authentication-with- Facebook的/ 但没有任何作用,所以必须有一些我看不到的其他错误。 唯一有效的方法是: ActiveRecord::Base.connection.execute(SQL query here) 它更新了db中的正确列,但这可能完全不是Ruby方式,它看起来很糟糕。 我在spree_social gem挖了,有这样的方法: def update_oauth_method auth_method = params[:authentication_method] if auth_method[:active] == “true” && auth_method[:environment] == ::Rails.env Devise.setup do |config| config.omniauth auth_method[:provider], auth_method[:api_key], auth_method[:api_secret] end end end 但我找不到任何方法将它保存到数据库中。 不幸的是我无法在配置/初始化器中设置断点,所以我无法完成它,因为我必须盲目地测试配置。

Ruby on Rails:在while循环中嵌套每个循环会产生错误

当我在rails控制台中执行以下代码时出现语法错误:“语法错误,意外$ end,期望keyword_end” range = 2 my_array = Array.new(range) a = [1] i = 0 while i < range do a.each do |b| puts "test" end i += 1 end 有谁知道我做错了什么? 奇怪的是,代码在ruby文件中的我的服务器上工作。 非常感谢! tuxware

Rails获取当前登录的Windows用户的用户名

我需要获取当前登录的Windows用户的用户名。 可以轻松完成吗?

ID在url中混淆了

所以我对Ruby on Rails比较陌生,正在建立一个项目管理系统,用于使用该语言在我的大学进行独立研究。 到目前为止,我创建了: 用户可以创建多个项目的区域(http://sofzh.miximages.com/ruby-on-rails/pXOBg.png) 在每个项目中,用户可以创建多个待办事项列表( http://sofzh.miximages.com/ruby-on-rails/FT3no.png ) 在每个待办事项列表中,用户可以创建多个任务,可以标记为完成/不完整(http://sofzh.miximages.com/ruby-on-rails/DMdcN.png) 一切都通过SQLite DB中的外键相关,并且工作相对较好,除了我添加任务时。 虽然任务确实被添加到待办事项列表中,但我被重定向到不正确的URL。 我创建了与我的项目管理系统分开的待办事项列表应用程序,只是为了看看我如何创建它以及重定向和一切工作正常,但将其实现到我的项目中已经certificate有点麻烦。 以下是遇到的问题: http : //i.imgur.com/Axuf2.jpg 。 URL中的链接应为http:// localhost:3000 / projects / 2 / lists / 15 ,因此ID会混淆。 显示的是http:// localhost:3000 / projects / 15 / lists / 14 ,其中15是列表的ID,而不是项目的ID,14是任务的ID,而不是列表。 我相信问题出在我的tasks_controller.rb中 ,其中包含以下内容: class TasksController < ApplicationController attr_accessor :completed before_filter :find_list respond_to :html, :xml, :js def […]