Posts

Showing posts with the label ruby-on-rails

VCR says “no cassette in use” even after specifying cassette

Image
Clash Royale CLAN TAG #URR8PPP VCR says “no cassette in use” even after specifying cassette I have an api-only RoR app with a user model. Users are authenticated via Twilio/Authy (using this gem). Each user has_one authy_user model for storing authy info, with dependent: :destroy . has_one authy_user dependent: :destroy The authy_user model has a before_destroy hook that connects to the authy api via the authy gem and deletes the user there. authy_user before_destroy The authy_user specs run just fine, and I've recorded cassettes for both registering and deleting users with the authy api. authy_user A piece of the authy_user spec: authy_user describe "delete user" do before do @user = create(:user_with_authy_user) end context "user is registered" do it "deletes user in authy and locally" do VCR.use_cassette("authy_delete_user") do expect { @user.authy_user.destroy }.to change { AuthyUser.count }.by(-1) ...

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...

How to download an uploaded file in Cloudinary

Image
Clash Royale CLAN TAG #URR8PPP How to download an uploaded file in Cloudinary i'm currently working on how to download an uploaded file in Cloudinary. In my views/declarations/index , I want a button to download the file the user uploaded at the Declaration.create . views/declarations/index Declaration.create I've tried the this : <% @declarations.each do |declaration| %> <tr> <td><%= link_to("Download", cl_private_download_url(declaration.facture, :png, :attachment => true)) %></td> </tr> <% end %> But all I have is something like this : #Attachinary::File:0x00007fa7deca0f20 Does one of you know how could I do this ? Many thanks, You need to link to a controller method that actually handles the file transfer. This method receives the declaration ID, pulls the record, fetches the file, and serves it. – bo-oz 3 hours ago ...

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...

When modifying a Rails app on Docker, how get source code changes to load?

Image
Clash Royale CLAN TAG #URR8PPP When modifying a Rails app on Docker, how get source code changes to load? When running a Rails app on a development machine, using Docker, how does once force code changes to be used? I'm using Docker to test out / tweak a Rails app (Helpy), but when I modify the Rails source any changes just get ignored. Even changes to dockker/run.sh or tweaking text on a view. So docker is apparently caching everything, how to I tell docker to use the current source code that I have edited? I tried docker-compose down (then up) or docker-compose restart or docker-compose build The Dockerfile is FROM ruby:2.4 ENV HELPY_VERSION=master RAILS_ENV=production HELPY_HOME=/helpy HELPY_USER=helpyuser HELPY_SLACK_INTEGRATION_ENABLED=true RUN apt-get update && apt-get upgrade -y && apt-get install -y nodejs postgresql-client imagemagick --no-install-recommends && rm -rf /var/lib/apt/lists/* && useradd --no-create-home $...

Streaming a .js.erb response

Image
Clash Royale CLAN TAG #URR8PPP Streaming a .js.erb response In this scenario, when the user submits his form, the $("body .my-flash-div") div should first show $("body .my-flash-div") Generating report then after a few seconds Sql executing so on and so forth. My issue is that the end user is shown the results at once, and not in a step by step manner. Here's my setup: End users want to download reports Report generation is a multi step process, takes 5-10 seconds. Step 1 user does GET /users_report/new There's a form there, user enters email address, submits And it's handled by class UsersReportController < ApplicationController def create respond_to do |format| format.js end end # app/views/users_report/create.js.erb $("body .my-flash-div").html("Generating report"); <% Namespace::gen_report_step_1 %> <% sleep 1 %> $("body .my-flash-div").html("Sql executing"); <% Namespac...

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...

Parse JSON with Ruby On Rails 5 usin API Key

Image
Clash Royale CLAN TAG #URR8PPP Parse JSON with Ruby On Rails 5 usin API Key Hello everyone i hope you are all well. I'm pretty new to Ruby on Rails and i find myself on a situation that i'm sure it will be easy for the majority of you but i'm struggling a bit. I have an API key and an API URL to retrieve data in JSON. How can i access these data from my rails application and finally display them? I ideally need all the steps required in bullet points to get an idea. It's a pretty open question but i'm mainly interested in the approach. 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.

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 to check if a number is within range in activerecord?

Image
Clash Royale CLAN TAG #URR8PPP How to check if a number is within range in activerecord? I have a model with a range column #<JobRequirement id: 1, age: 18...30> How can I get the JobRequirement using an age: 20 ? JobRequirement age: 20 Something like JobRequirement.where(age: 20) Which database and what type is age inside the database? – mu is too short 11 hours ago age It is int4range in postgreSQL – Brian Law 11 hours ago 1 Answer 1 I think you need to use the PostgreSQL range operators and a bit of SQL in a string. In particular, you'd want the @> (contains element) operator: @> JobRequ...

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...