Posts

Showing posts with the label ruby

Autoload paths and nested services classes crash in Ruby

Image
Clash Royale CLAN TAG #URR8PPP Autoload paths and nested services classes crash in Ruby I've multiple issues to load / require classes under my app/services folder in a Rails 5 project and I'm starting to give up on this issue. app/services First of all and to be clear, services/ are simple PORO classes I use throughout my project to abstract most of the business logic from the controllers, models, etc. services/ The tree looks like this app/ services/ my_service/ base.rb funny_name.rb my_service.rb models/ funny_name.rb First, when I tried to use MyService.const_get('FunnyName') it got FunnyName from my models directory. It does not seem to have the same behavior when I do MyService::FunnyName directly though, in most of my tests and changes this was working fine, it's odd. MyService.const_get('FunnyName') FunnyName MyService::FunnyName I realised Rails config.autoload_paths does not load things recursively ; it would makes sense that t...

undefined method `reset' for RDoc::TopLevel:Class when installing a new Ruby gem

Image
Clash Royale CLAN TAG #URR8PPP undefined method `reset' for RDoc::TopLevel:Class when installing a new Ruby gem For example, $ gem install netaddr Fetching: netaddr-1.5.1.gem (100%) Successfully installed netaddr-1.5.1 ERROR: While executing gem ... (NoMethodError) undefined method `reset' for RDoc::TopLevel:Class The NoMethodError exception keeps being raised when I install any new gem. I googled about it and searched issues in RDoc and RubyGems GitHub repositories, but had no luck. My Ruby version is $ ruby -v ruby 2.3.0p0 (2015-12-25 revision 53290) [x86_64-darwin15] The RubyGems version is $ gem -v 2.5.1 The installed RDoc version is $ gem search rdoc -l *** LOCAL GEMS *** rdoc (4.2.2, 4.2.1) I installed Ruby with RVM on Mac OS X El Capitan. Thanks! I have exactly the same environment as you do, except for ruby version (mine is ruby 2.3.1p112 (2016-04-26 revision 54768) [x86_64-darwin15] ), and had no problem installing the gem. ...

Getting null value using map in Rails

Image
Clash Royale CLAN TAG #URR8PPP Getting null value using map in Rails I am getting null value when I am using if condition inside a map. I have an array and I looping through that. My first array is: [ { id: 5, Vegetables: "Cabbage", Area: 39, Production: 695.33, Year: 2014, created_at: "2018-07-18T06:23:11.000Z", updated_at: "2018-07-18T06:23:11.000Z" }, { id: 12, Vegetables: "Bittergourd", Area: 9.71, Production: 67.25, Year: 2014, created_at: "2018-07-18T06:23:11.000Z", updated_at: "2018-07-18T06:23:11.000Z" }, ..... ] This is my array and and I using this code to loop: ji1 = ["Cabbage","Bittergourd"] hash_data = ji1.map do |col| dataset = col.to_s.gsub("_"," ") { type:views, legendText: dataset, ...

Why Net::HTTPUnknownResponse.kind_of? Net::HTTPResponse returns false?

Image
Clash Royale CLAN TAG #URR8PPP Why Net::HTTPUnknownResponse.kind_of? Net::HTTPResponse returns false? I am using the ruby's native library net/http and trying to check if a response is the type Net::HTTPResponse so the test is equivalent to: net/http Net::HTTPResponse Net::HTTPUnknownResponse.kind_of? Net::HTTPResponse which is returning false . What's wrong? false Ruby: 2.4.1 By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

The action 'show' could not be found for CommentsController - Ruby Tutorial

Image
Clash Royale CLAN TAG #URR8PPP The action 'show' could not be found for CommentsController - Ruby Tutorial I am trying to follow the following guide for starting Ruby on Rails. Everything went well until I try to destroy (delete comments). I receive the following error: The action 'show' could not be found for CommentsController I will post my code below. http://guides.rubyonrails.org/getting_started.html comments_controller.rb class CommentsController < ApplicationController http_basic_authenticate_with name: "dhh", password: "secret", only: :destroy def create @article = Article.find(params[:article_id]) @comment = @article.comments.create(comment_params) redirect_to article_path(@article) end def destroy @article = Article.find(params[:article_id]) @comment = @article.comments.find(params[:id]) @comment.destroy redirect_to article_path(@article) end private de...

converting time to digestible JSON string with HTTParty

Image
Clash Royale CLAN TAG #URR8PPP converting time to digestible JSON string with HTTParty The following string returns results @result = HTTParty.post( 'https://test.co.uk/search', :body => '{ "requestAuditInfo": { "agentCode": "MLG001", "requestDateTime": "2018-07-29T07:03:38Z" [...] whereas @result = HTTParty.post( 'https://test.co.uk/search', :body => '{ "requestAuditInfo": { "agentCode": "MLG001", "requestDateTime": Time.now.utc.iso8601 [...] returns nil Console for Time.now.utc.iso8601 returns "2018-07-29T07:03:38Z" which is the desired string for the successful call. Time.now.utc.iso8601 "2018-07-29T07:03:38Z" What am I getting wrong? update :body => "{ 'requestAuditInfo': { 'agentCode': 'MLG001', 'requestDateTime': '2018-07-29T07:03:38Z' is NOT being digested by the receiving...

Carrierwave: Encoding::UndefinedConversionError (“xFF” from ASCII-8BIT to UTF-8)

Image
Clash Royale CLAN TAG #URR8PPP Carrierwave: Encoding::UndefinedConversionError (“xFF” from ASCII-8BIT to UTF-8) I'm trying to upload multiple files to a record called Post , with a json attribute called images , using Carrierwave. But when I try to save the record, I get this error: Post images (0.5ms) ROLLBACK Completed 401 Unauthorized in 15ms (ActiveRecord: 0.8ms) Encoding::UndefinedConversionError ("xFF" from ASCII-8BIT to UTF-8): I've used Carrierwave before and I never got this error. I checked if anyone's had this problem, before, and I didn't find much. This question suggested the activesupport-json_encoder gem, but this gem isn't compatible with Rails 5. Also suggested were the force_encoding method and using a "wb" flag when saving the file, but the Carrierwave code has no place to implement those suggestions. activesupport-json_encoder force_encoding Here's the code I have: form <%= form_for @post, :html => {multipart: t...

How do I get the (html) id from the select tag on rails_admin?

Image
Clash Royale CLAN TAG #URR8PPP How do I get the (html) id from the select tag on rails_admin? The 'rails_admin' generate this HTML below, I need to change the options of another 'select' tag depending on which 'option' is selected in this 'select' tag, with a coffescript/jquery. The id= of this 'select' tag is "schedule_tasks_attributes_1532831082497_service_id". How do I get this id with this number inside? This is not a fixed number. <div class="form-group control-group belongs_to_association_type service_field "id="schedule_tasks_attributes_1532831082497_service_id_field"> <label class="col-sm-2 control-label" for="schedule_tasks_attributes_1532831082497_service_id">Service</label> <div class="col-sm-10 controls"> <select data-filteringselect="true" data-options="{&quot;xhr&quot;:false,&quot;remote_source&quot;:&quo...

instantiate Foo with ruby? [duplicate]

Image
Clash Royale CLAN TAG #URR8PPP instantiate Foo with ruby? [duplicate] This question already has an answer here: How do I instantiate Foo from bar.rb ? bar.rb # foo.rb class Foo def initialize puts "foo" end end # bar.rb require 'foo' Foo.new $ ruby bar.rb /home/thufir/.rvm/rubies/ruby-2.4.1/lib/ruby/site_ruby/2.4.0/rubygems/core_ext/kernel_require.rb:55:in `require': cannot load such file -- foo (LoadError) from /home/thufir/.rvm/rubies/ruby-2.4.1/lib/ruby/site_ruby/2.4.0/rubygems/core_ext/kernel_require.rb:55:in `require' from bar.rb:2:in `<main>' Not using modules at the moment. Works fine when Foo is declared inside the same script: Foo # bar.rb with Foo declared inside class Foo def initialize puts "foo" end end Foo.new $ ruby bar.rb foo This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question. 1...

Exit pry session by alias command

Image
Clash Royale CLAN TAG #URR8PPP Exit pry session by alias command I wanna exit pry session by q instead of exit! q exit! Both are not working for me. Pry.commands.alias_command 'q', 'exit!' Pry.commands.rename_command 'q', 'exit!' 2 Answers 2 This works for me: Pry.commands.alias_command :q, "!!!" Grych's answer works great for me. Just wanted to point out that the alias_command method takes a third argument, which allows you to add a description for the alias: alias_command Pry.commands.alias_command :q, :exit, 'a helpful and poetic description for exiting from pry' Pry.commands.alias_command :q, :exit, 'a helpful and poetic description for exiting from pry' By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie poli...

Best practice regarding controllers for a resource that can have different parent classes

Image
Clash Royale CLAN TAG #URR8PPP Best practice regarding controllers for a resource that can have different parent classes I'm currently trying to refactor some controller code and I came accross some code that I'm not sure how to implement in a correct way. My application has users and companies, and both can have projects. The current situation is that we have 2 urls: Those will route to the same controller which will handle the request differently based on if a company_id exists or not. This is not very clean in my opinion so I have been thinking about a better way to do this. So far, I think the best way is to split them up in seperate controllers, like: But since the only difference between a user project and a company project is pretty much that one has a 'user_id' and the other has a 'company_id', it feels like that will not be very DRY as I'll be writing a lot of duplicate code. The current solution probably isn't as much of a problem, but I want t...

Script to keep only the last three directories under each of the subdirectory

Image
Clash Royale CLAN TAG #URR8PPP Script to keep only the last three directories under each of the subdirectory Assume that I have a directory structure as follows: applications │ ├── configs │ ├── 1.0.0.1 │ └── 1.0.2.1 │ ├── logs │ ├── l1.0.3 │ └── l2.0.4 │ └── src ├── s1.p.8 ├── s1.p.9 └── s2.p.10 Now I want to delete the folders under each subdirectory under applications directory and keep only the last three under each subdirectory Eg: under applications directory we have like configs, logs,src, test, etc. The requirement is that I need to keep only the last three directories under each of these directories(configs, logs,src, test, etc). Thanks. By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

How to serve different web apps using one domain name in Apache?

How to serve different web apps using one domain name in Apache? I'm currently trying to deploy my Django 2.0 App on an Apache Web Server. However I'm experiencing difficulties in configuring it because my Virtual Host configuration overrides other Virtual Hosts which is used by other projects (Ruby and PHP WebApps). We only have one domain name at the moment so I cannot use other domain names to host my app. Is it possible to serve different kind of apps with one domain name using Apache Virtual Hosts? Yes it is possible. – Nic3500 yesterday Can please give me an example of a Virtual Host Configuration? I'm really confused with the ones in the Apache website. – nixon 10 hours ago ...