Tag: ruby on rails 5

Ruby嵌套表单

实际上,在保存它必须保存,但我收到错误,并试图找出我错误的错误。 请帮我解决这个错误。 提前致谢。 调节器 def new @fooditem = Fooditem.new 3.times { @fooditem.fooditemprices.build} end #Creating Food Items def create @fooditem = Fooditem.new(fooditem_params) if @fooditem.save flash[:success] = “Food item created successfully.” redirect_to fooditems_path else render ‘new’ end end 楷模) class Fooditem < ApplicationRecord has_many :fooditemprices, dependent: :destroy accepts_nested_attributes_for :fooditemprices, reject_if: lambda {|attributes| attributes['price'].blank?}, allow_destroy: true end class […]

LoadError:无法加载’listen’gem(Rails 5)

我有一个API模式Rails 5应用程序,不会让我运行rake routes或rails s 。 我得到的错误是: $ rake routes rake aborted! LoadError: Could not load the ‘listen’ gem. Add `gem ‘listen’` to the development group of your Gemfile …/config/environment.rb:5:in `’ LoadError: cannot load such file — listen …/config/environment.rb:5:in `’ Tasks: TOP => routes => environment (See full trace by running task with –trace) 我已经validation了listen在我的Gemfile中的开发组中: group […]

Rails:belongs_to,条件/范围引发语法错误

我想选择拥有的公司。 我尝试了多种组合, 新的,rails 3,旧学校……所有这些都抛出相同的语法错误unexpected ‘\n’, expecting => belongs_to :from, class_name: ‘Company’, foreign_key: ‘from_id’, -> { where owned: true } belongs_to :from, class_name: ‘Company’, foreign_key: ‘from_id’, -> { where(owned: true) } belongs_to :from, class_name: ‘Company’, foreign_key: ‘from_id’, -> { where(:owned => true) } belongs_to :from, class_name: ‘Company’, foreign_key: ‘from_id’, condition: { where(:owned => true) } 3年前似乎有人问过这里 […]

rails动态在哪里sql查询

我有一个对象,有一堆属性代表可搜索的模型属性,我想只使用设置的属性动态创建一个SQL查询。 我创建了下面的方法,但我相信它容易受到sql注入攻击。 我做了一些研究并阅读了rails活动记录查询界面指南,但似乎where条件总是需要一个静态定义的字符串作为第一个参数。 我还试图找到一种方法来清理我的方法生成的sql字符串,但似乎没有一种好的方法可以做到这一点。 我怎么能做得更好? 我应该使用where条件还是以某种方式清理这个sql字符串? 谢谢。 def query_string to_return = “” self.instance_values.symbolize_keys.each do |attr_name, attr_value| if defined?(attr_value) and !attr_value.blank? to_return << "#{attr_name} LIKE '%#{attr_value}%' and " end end to_return.chomp(" and ") end

Rails 5模型对象未显示所有Devise属性

我正在使用Rails 5 API和设计。 我有一个User模型。 Schema看起来像这样。 create_table “users”, force: :cascade do |t| t.string “email”, default: “”, null: false t.string “encrypted_password”, default: “”, null: false t.string “reset_password_token” t.datetime “reset_password_sent_at” t.datetime “remember_created_at” t.integer “sign_in_count”, default: 0, null: false t.datetime “current_sign_in_at” t.datetime “last_sign_in_at” t.inet “current_sign_in_ip” t.inet “last_sign_in_ip” t.datetime “created_at”, null: false t.datetime “updated_at”, null: false t.index [“email”], name: “index_users_on_email”, […]

正在使用Rails 5 ActionCable

我正在玩新的ActionCablefunction。 有没有办法使用say socket.io或使用React或ReactNative从应用程序与ActionCable服务器通信? 也许我对ActionCable的正确使用感到困惑,它并不打算用作代替API的替代品,而是用作同一应用程序的支持前端技术。 如果可能的话,任何使用ActionCable作为独立WS服务器的好例子或指南都将受到赞赏。

Rspec测试失败,truenthicate_client无法正常工作

我正在将遗留项目升级到rails 5,并且在失败的rspec测试中我有一个说: Failure/Error: expect(response).to be_redirect expected `#<ActionDispatch::TestResponse:0x00007fbe5fde51f0 @mon_owner=nil, @mon_count=0, @mon_mutex=#<Thread::…ch::Http::Headers:0x00007fbe5fdde9e0 @req=#>>>.redirect?` to return true, got false # ./spec/controllers/search_controller_spec.rb:86:in `block (3 levels) in ‘ 我正在使用devise gem来validation客户端。 测试如下: describe SearchController do before(:each) do @client = FactoryGirl.create(:client) end describe “authenticated search” do # a bunch of tests end describe “unauthenticated search” do it “requires a user to be […]

在控制器(ApplicationMailer)中的S3上打开远程映像 – 没有这样的文件或目录@ rb_sysopen

我正在尝试打开通过Paperclip上传到S3的个人资料图片,以便将其作为电子邮件的在线附件提交。 虽然我收到错误: No such file or directory @ rb_sysopen 这是我的一些代码: attachments.inline[‘profilepic’] = File.read(profilepic) profilepic是一个绝对的URL(以//mybucket.S3-eu-west …..开头)到S3的图像(当粘贴到导航栏上时,它只是完美地显示图像) 我尝试使用open-uri进行以下操作,但同样的错误 require ‘open-uri’ attachments.inline[‘profilepic’] = open(profilepic)

Rails 5 ask_as_taggable with select2

我无法看到select2在rails 5中的效果,无论是text_field还是select。 有谁知道为什么没有填充tag_list。 我可以看到tag_list有值,但正如您在下面的快照中看到的那样,它显示“未找到结果”。 select2是否适用于Rails 5,因为我尝试了几个选项? form:选择all将在框中显示所有值 Select All 我使文本字段变得简单而不是select2,并且能够通过将:tag_list添加到强参数来在DB中添加标签。 application.js – 注意:select2没有用o我使用了select2-full,我在谷歌的某个地方看过。 //= require underscore //= require select2-full select2的JS代码 $(function() { $(‘input#post_tag_list’).select2({tags:[], placeholder: “Choose a Tag”, allowClear: true}) }); 现在,我检查了普通的HTML和JS它甚至使用select2(或select2-full) Apple Banana Grapes Select All 相应的JS代码是: $(“#checkbox”).click(function(){ if($(“#checkbox”).is(‘:checked’) ){ $(“#post_topic_list > option”).prop(“selected”,”selected”); $(“#post_topic_list”).trigger(“change”); }else{ $(“#post_topic_list > option”).removeAttr(“selected”); $(“#post_topic_list”).trigger(“change”); } }); 有人可以指出普通的HTML选择输入是否与select2一起工作为什么它在rails 5中没有相同的function? 我尝试了bootstrap_form_for和form_for。 我甚至读过有关turbolinks问题的内容……如何解决turbolinks没有加载select2的问题?

如何手动解密Rails 5会话cookie?

我有权访问 config.action_dispatch.encrypted_cookie_salt config.action_dispatch.encrypted_signed_cookie_salt secrets.secret_key_base 完整的cookie字符串(包括– ) 我在Rails 4中看到了这样做的方法( Rails 4:如何解密rails 4会话cookie(给定会话密钥和秘密) ),但这些似乎在Rails 5中不起作用。