Tag: ruby on rails 4

rails console无法找到已安装的gem

$ bundle install |grep chunk Using chunky_png 1.3.3 $ rails c Could not find chunky_png-1.3.3 in any of the sources Run `bundle install` to install missing gems. 我已经尝试从rvm gemset中删除所有gem并重新运行捆绑两次,但没有帮助。 有任何想法吗? 这是在Ubuntu 13.10机器上。 我的Mac OSX机器上的相同代码工作正常。

Rails 4:从自定义validation器中的错误消息中删除属性名称

在我的Rails 4应用程序中,我在Post模型上实现了一个名为LinkValidator的自定义validation器: class LinkValidator < ActiveModel::Validator def validate(record) if record.format == "Link" if extract_link(record.copy).blank? record.errors[:copy] << 'Please make sure the copy of this post includes a link.' end end end end 一切正常,但目前显示的信息是: 1 error prohibited this post from being saved: Copy Please make sure the copy of this post includes a link. 如何从上述消息中删除“复制”一词? 我在validation器中尝试了record.errors << […]

Ruby on Rails:通过属性而不是id查找记录

我对rails非常陌生,所以请耐心等待我。 总之,我正在尝试创建一个表格,其中婚礼的客人可以输入简单的代码( invite_code ),然后回复。 from应该使用invite_code ,然后直接使用正确的invites#show invite_code视图。 到目前为止一切都那么好,但是我一直试图让轨道通过除了id以外的东西找到记录,我想通过invite_code找到。 假设我有一个id为4且invite_id为1234的invite_id ,当我输入’4’而不是’1234’时,表单找到了正确的记录。 这里有一些代码可以解释: 的routes.rb get ‘invites/search’, to: ‘invites#show’, controller: :invites 形成 … … invites_controller … def show if params.has_key?(:invite_code) @invite = Invite.find(params[:invite_code]) else @invite = Invite.find(params[:id]) end end … 耙路线输出 Prefix Verb URI Pattern Controller#Action info_index GET /info/index(.:format) info#index invites GET /invites(.:format) invites#index POST /invites(.:format) invites#create new_invite […]

如何使用多对多关系post#show在ruby on rails

在团队#model中 class Team :postteams end 在Post#model中 class Post :postteams accepts_nested_attributes_for :postteams end 在Postteam模型中 class Postteam < ActiveRecord::Base belongs_to :post belongs_to :team end 通过这些关系,我终于成功发布了这些多个团队,并将这些团队保存在数据库中,就像这样 1 这是我的post#controller : class PostsController team) end end respond_to do |format| if @post.save format.html { redirect_to @post, notice: ‘post was successfully created.’ } format.json { render :show, status: :created, location: @post } […]

改进API调用和响应

TheChamp帮我解决了一系列哈希问题。 谢谢。 现在我想改进一下。 观点真的很混乱 将代码移动到模型,因此只调用实例变量将显示所需的结果。 就像航class类型的一个变量,出发时间,价格结构等。 根据某些条件,将结果排序在模型本身中。 与价格一样,默认为最低价。 获取API响应需要花费大量时间。 缓存响应的各种选项有哪些,因此结果是即时的。 此外,需要检查哪些值以确保缓存是新鲜的。 这是我的代码库。 (供您考虑,API响应的一部分 – http://jsfiddle.net/PP9N5/ ) 模型 class Cleartrip include HTTParty debug_output $stdout base_uri “api.staging.cleartrip.com/air/1.0/search” headers ‘X-CT-API-KEY’ => ‘xxxxxxxxxxxxxxxxxxxxxxxxxxx’ format :xml def self.get_flight response = get(“?from=DEL&to=BLR&depart-date=2014-08-10&adults=1&children=0&infants=0”) if response.success? response else raise response.message end end 调节器 # This does nothing expires_in 20.minutes, public: true @flight = […]

在Active Admin Gem中,如何修改资源视图?

我在Rails项目中实现了ActiveAdmin。 我有一个名为“客户”的资源。 在导航栏中,有一个名为“新客户”的选项来创建客户。 如何更改此新客户的视图? 当我点击“新客户”按钮时,它必须接受来自我的电子邮件,它应该发送该电子邮件ID的邀请。

Rails命令错误

我即将开始在另一个Web应用程序上工作,我将目录更改为 / rails_projects 并输入 铁路新空白 然后我得到这个错误 Error: Command not recognized Usage: rails COMMAND [ARGS] The common rails commands available for engines are: generate Generate new code (short-cut alias: “g”) destroy Undo code generated with “generate” (short-cut alias: “d”) All commands can be run with -h for more information. If you want to run any commands […]

播种未通过嵌套表validation(validates_presence_of)

组织模型与用户模型具有1:多的关联。 我的用户模型文件中有以下validation: belongs_to :organization validates_presence_of :organization_id, :unless => ‘usertype==1’ 如果usertype为1,则表示用户没有与之关联的组织。 对于不同的usertype,organization_id的存在应该是强制性的。 组织模型包括: has_many :users accepts_nested_attributes_for :users, :reject_if => :all_blank, :allow_destroy => true 我的种子文件使用嵌套,包括: Organization.create!(name: “Fictious business”, address: Faker::Address.street_address, city: Faker::Address.city, users_attributes: [email: “helpst@example.com”, username: “helpyzghtst”, usertype: 2, password: “foobar”, password_confirmation: “foobar”]) 播种时会产生以下错误。 从模型中删除validation解决了它,但我不想这样做。 我怎么解决这个问题? validation失败:用户组织不能为空

使用Geocoder gem通过IP搜索位置无法正常使用rails 4

我在我的rails应用程序中添加了geocoder gem,但是当我通过IP搜索位置时它没有正确响应。 问题是它返回的是nil数组。 Geocoding API’s response was not valid JSON. [] 但有时候它有效,我不知道为什么。 我做了什么: 的Gemfile gem ‘geocoder’ 初始化/ geocoder.rb Geocoder.configure( :timeout => 20, :lookup => :google, #:ip_lookup => :google, #:api_key => “xxxx-xxx”, :units => :km ) application_controller.rb def current_location if Rails.env.development? geo_data = Geocoder.search(‘50.131.44.114’) else geo_data = Geocoder.search(request.remote_ip) end [geo_data[0].city, geo_data[0].country_code ].compact.join(‘, ‘) end

Rails 4 app进入Bluehost的问题

我在启动我在bluehost上托管的rails应用程序时遇到了一些问题。 我认为问题是乘客版本与rails 4应用程序交互,但我不确定。 当我启动我的应用程序时,我得到了这个跟踪 Ruby (Rack) application could not be started These are the possible causes: There may be a syntax error in the application’s code. Please check for such errors and fix them. A required library may not installed. Please install all libraries that this application requires. The application may not be properly configured. […]