Tag: ruby

动态调整Active Admin表单上的下拉字段(rails 3.2)

我在Ruby on Rails应用程序上有一个非常基本的架构: 公司模式 行业模式(属于行业的公司) 子行业模式(子行业属于行业) 在我的活动管理表单页面上,我想放置字段,以便对于每个公司,我首先选择它所属的行业的下拉(这到目前为止工作),然后选择另一个字段“子行业”,它只会显示根据我在之前的领域中选择的行业,我在此行业中已经分类过的子行业。 例如,古柯将进入行业“饮料和饮料”,我希望动态调整forms,只显示在“sub indsutry”领域:“热饮”,“冷饮”,“茶”的下拉菜单,“苏打水” form do |f| f.inputs “Company” do f.input :company_industry_id, :label => “Select industry:”, :as => :select, :collection => CompanyIndustry.all f.input :company_subindfustry_id, :label => “Select subindustry:”, :as => :select, :collection => CompanySubindustry.all end 显然我到目前为止遇到了一个问题,它向我展示了我所拥有的所有子行业,而不仅仅是我在前一个领域选择的行业中的子行业。 有谁知道我怎么解决这个问题?

为什么twitter oauth访问令牌无效/过期(rails 3)

过去三天我一直试图解决这个问题,但无济于事。 我所要做的就是允许使用Twitter登录的用户向他们的帐户发推文。 这是我要解决的错误消息: Twitter::Error::Unauthorized in PostsController#create Invalid or expired token 我尝试了各种不同的东西,但我回到了完整的360圈到同一条消息。 这就是我现在所处的位置: class ApplicationController provider.token, :secret => provider.secret) rescue nil end @twitter_user end 这是抛出错误的地方 class PostsController < ApplicationController def create twitter.update(@post.content) end 这是模式的布局方式 create_table “authentications”, :force => true do |t| t.integer “user_id” t.string “provider” t.string “uid” t.datetime “created_at”, :null => false t.datetime “updated_at”, :null => […]

如何通过其他模型创建集成测试

1)我有这个模型Job和模型机构 class Job < ApplicationRecord belongs_to :institution # others attibutes end 2)这是我在JobsController上创建的动作 – 我需要一个机构来创建一份工作。 没事。 def create build_job save_job || render(:new, status: :unprocessable_entity) end 3)这是我创建的集成测试我没有得到成功测试 在params -I also tried institution: @institution -and also tried institution_id: @institution.id require ‘test_helper’ class JobActionsTest < ActionDispatch::IntegrationTest setup do @user = users(:standard) sign_in @user @institution = institutions(:standard) end test "can […]

如何获取实际的用户名而不是#(ruby on rails 3)

在我看来,我用它来显示用户 Made a comment on 当我这样做时,它可以工作,但链接显示为# 我尝试使用activity.trackable.micropost.user.username , activity.trackable.micropost.user.name和其他变体,但它们不起作用。 在.user之后我需要添加什么? activity.trackable来自PublicActivity gem。

可以将表单添加到rails中的另一个模型视图中

我有一个非常小的应用程序我在rails中构建。 这是一个简单的重量跟踪应用程序。 我创建了一个具有注册页面的User模型。 用户登录后,会将其重定向到user#show视图。 到目前为止,这是用户控制器: class UsersController < ApplicationController before_filter :authenticate_user! def show @user = current_user end end 我有2个其他模型,一个是Weight模型,另一个是Goal模型,我想它,所以当用户注册时,他们会看到一个屏幕,要求他们输入当前体重和目标体重这个信息将是分别存储权Weight和Goal模型以及登录用户ID。 到目前为止,我已经能够向用户show.html.erb模板添加一个表单: 这会正确呈现表单但当我单击提交按钮时,它只会转到错误页面,说明Unknown action- The action ‘update’ could not be found for UsersController 。 我假设iM做错了,因为它应该尝试发送到创建操作。 有没有人可以帮助我回到正确的道路上,我非常喜欢铁轨上的菜鸟。

从范围记录计算总数百分比并显示虚拟属性(Rails / Active Record)

创建一个范围,可能是这样的.. scope :mv, select(‘*,quantity*market_price as market_value, quantity*market_price/sum(quantity*market_price) as percent’) 这会创建两个虚拟属性,market_value和percent。 我遇到的问题是创建包含sum()的百分比。 如果我添加sum(),则范围返回一条记录。 我需要计算相对于范围记录集的总市值的market_value的百分比。 例: 1, market_value: 100, percent: .10 2, market_value: 100, percent: .10 3, market_value: 100, percent: .10 4, market_value: 100, percent: .10 5, market_value: 100, percent: .10 6, market_value: 500, percent: .50 Total is 1000 但是,如果我将其范围扩大到market_value <6,我应该看到 1, market_value: 100, percent: .20 2, […]

问题在Highchart脚本中有空白图形

我按照http://www.highcharts.com/demo/column-basic/skies中的步骤操作,问题是我的控制器到我的Highchart图形的值显示为空白(未显示我的值) 在我的桌子http://sqlfiddle.com/#!2/b7347/5 ,我在我的控制器中使用 |policies| ID POLICY_NUM DATE_INI CATEGORY_ID 1 1234 2013-01-10 1 2 5678 2013-01-10 2 3 3444 2013-02-10 1 4 4577 2013-02-10 2 |categories| ID NAME 1 Life 2 Vehicles 这是我的控制器 def report @jan_life =Policy.find_by_sql(“select count(*) as total from policies Inner join categories ON categories.id =category_id WHere category_id = 1 AND date_ini BETWEEN ‘2013-01-01’ […]

skip_before_filter用于设计的“登录”操作

我可以这样做,在我需要的某个控制器中跳过调用“authenticate_user”: class ApplicationController before_filter :authenticate_user! end class MyController < ApplicationController skip_before_filter :authenticate_user! end 我想调用skip_before_filter来设计skip_before_filter的“login”操作,我不想重写任何其他内容。 我怎样才能做到这一点?

如何将Ruby函数放入SQLite3查询中?

我有一个函数,我需要将其放入SQLite3查询中。 我有以下方法: def levenshtein(a, b) case when a.empty? then b.length when b.empty? then a.length else [(a[0] == b[0] ? 0 : 1) + levenshtein(a[1..-1], b[1..-1]), 1 + levenshtein(a[1..-1], b), 1 + levenshtein(a, b[1..-1])].min end end 我想做一个看起来像这样的查询: @results = The_db.where(‘levenshtein(“name”, ?) < 3', '#{userinput}') 我想在The_db中找到name的值,其中name列的值与用户输入之间的编辑距离小于3 。 问题是我不知道如何在查询中使用Ruby函数。 这有可能吗?

为什么搜索结果的分页不起作用?

骑在前一个问题上: Ruby on rails – 搜索结果的分页 这是我的模特: def self.search(title, company, location_id) if location_id.present? paginate :conditions => [‘title LIKE ? AND company LIKE ? AND location_id = ?’, “%#{title}%”, “%#{company}%”, location_id].last(200), :page => @page, :per_page => 20, :order => “total DESC” else paginate :conditions => [‘title LIKE ? AND company LIKE ?’, “%#{title}%”, “%#{company}%”].last(200), :page => […]