jeudi 29 avril 2021

You've tried to invoke Spring when it's already loaded (i.e. the Spring constant is defined)

This is probably because you generated binstubs with Spring 1.0, and you now have a Spring version > 1.0 on your system. To solve this, upgrade your bundle to the latest Spring version and then run bundle exec spring binstub --all to regenerate your binstubs. This is a one-time step necessary to upgrade from 1.0 to 1.1.

Here's the backtrace:

/Users/m/Image-Classifier/vendor/bundle/ruby/2.7.0/gems/spring-2.1.1/lib/spring/binstub.rb:11:in load' /Users/m/Image-Classifier/vendor/bundle/ruby/2.7.0/gems/spring-2.1.1/lib/spring/binstub.rb:11:in <top (required)>' /Users/m/.rvm/rubies/ruby-2.7.1/lib/ruby/2.7.0/rubygems/core_ext/kernel_require.rb:92:in require' /Users/m/.rvm/rubies/ruby-2.7.1/lib/ruby/2.7.0/rubygems/core_ext/kernel_require.rb:92:in require' /Users/m/Image-Classifier/bin/spring:15:in <top (required)>' /Users/m/Image-Classifier/bin/rails:3:in load' /Users/m/Image-Classifier/bin/rails:3:in `'

I try with Spring and middleware conflict? but don't worked

to show first the comment left by the current_user using ORDER BY CASE

Into the pages where I show the comments I noted that the previous developer used this system (very good and it works perfectly)

in comments_controller.rb there is this

  def index
    @comments = movie.comments.roots.latest
                    .includes(:user).page(params[:page])
  end

in views/comments/index.html.erb there is this

The Comments
<div id="commentsContainer">
    <%= render partial: 'comment', collection: @comments %>
    <%= paginate @comments %>
</div>

and in view/comments/comment.html.erb there is this

<div id=<%= dom_id(comment) %>>
    <!-- Author -->
    <%= link_to comment.username, user_path(comment.username) %>
    <!-- Body Comment -->
    <%== parse_markdown(comment.text) %>
</div>

Now are showing the latest comments first.

I have to edit it to show first the comment left by the user (logged) that is watching the page and then the latest comments

So I should check if there is a comment left by the current_user and then to show it first. Should I use order by case? how to do?

how can i use .verify in verifying jwt token instead of .decode method in rails?

    def self.decode(token)
    decoded = JWT.decode(token, SECRET_KEY, true)
    HashWithIndifferentAccess.new decoded
    end

I am using the .decode method here to decode the key, when I use .verify instead of .decode it gives me an "undefined method 'verify'" error.

mardi 27 avril 2021

Pros and cons of a Hash vs a table

I am creating an application where I match mentees to mentors based on the mentee's major and the mentor's occupation. I am debating if I should have a hash that maps majors to occupations or if I should store this information in a table in the database. It is a many to many relationship. I would appreciate any feed back on the pros and cons of each data structure. Thank you so much.

jeudi 22 avril 2021

How to find url using controller and action of aliased route

I can find the URLs of most routes using url_for as below

url_for(controller: 'main', action: 'index')

But some of the routes in my application are aliased as

resources :car_insurance_leads, :path=>'car-insurance'

So when I try to find the URL for such a route using

url_for(controller: 'car_insurance_leads', action: 'index')

It gives me incorrect route /blog?controller=car_insurance_leads&action=index

Is there a way to find the URL for aliased routes in Rails 3?

mercredi 21 avril 2021

Rails schema dump says method_missing_with_paginate

I've got a Rails app I've updated to 3.1. I'm trying to run for the first time. So I'm attempting to build the database schema for the first time.

But I'm getting this error "method_missing_with_paginate" for class "Class"...

fonso@mybox:~/my-dev$ rake db:schema:dump
/home/fonso/fgdb-dev/vendor/soap4r/xsd/iconvcharset.rb:9:in `<top (required)>': iconv will be deprecated in the future, use String#encode instead.
DEPRECATION WARNING: ActiveRecord::Associations::AssociationCollection is deprecated! Use ActiveRecord::Associations::CollectionProxy instead. (called from <top (required)> at /home/fonso/fgdb-dev/config/application.rb:16)
DEPRECATION WARNING: ActiveRecord::Associations::AssociationCollection is deprecated! Use ActiveRecord::Associations::CollectionProxy instead. (called from <top (required)> at /home/fonso/fgdb-dev/config/application.rb:16)
rake aborted!
NameError: undefined method `method_missing_with_paginate' for class `Class'
/home/fonso/fgdb-dev/config/application.rb:16:in `<top (required)>'
/home/fonso/fgdb-dev/Rakefile:4:in `<top (required)>'
(See full trace by running task with --trace)

Here is my gemfile

source 'http://rubygems.org'

gem 'rails', '3.1.0'

gem 'pg', '~> 0.10.1pre209'


gem 'will_paginate', '~> 2.3.17'
gem 'soap4r-middleware', '~> 0.8.6'
gem 'soap4r',  '~> 1.5.6'
gem 'prawn-labels', '~> 0.11.3.0'
gem 'nokogiri-plist', '~> 0.3.0'
gem 'rails_sql_views', '~> 0.8.0' #all versions (0.1.0 - 0.8.0)of this crap require ruby 2.3. we can not use 2.3
gem 'prototype_legacy_helper', '0.0.0', :git => 'https://github.com/rails/prototype_legacy_helper.git'

# config.gem 'pg', '~> 0.20.0'
# pg 0.20.0 may be the version needed to overcome the t.tgisconstraint error. Only works with ruby 2.0
# can we use this..https://rubygems.org/gems/postgres-pr ??

"method_missing_with_paginate" makes me think this has something to do with will_paginate.

Can anyone point me in the right direction on fixing this?

How can I prevent SQL injection in following rails query?

Data is a param in the below statement :

condition = params["id"].present? ? "employers.status = '#{params["id"].upcase}' and employers.task = '#{data.upcase}'" : "employers.task.rdu = '#{data.upcase}'"