vendredi 14 août 2015

i get error omniauth: (stripe_connect) Authentication failure! invalid_credentials: OAuth2::Error, invalid_client: No such API key: Bearer

i tested my app locally and works fine, but then i deployed it to Heroku and no im running into issues. i get this error after i skip the form for redirection: ERROR -- omniauth: (stripe_connect) Authentication failure! invalid_credentials: OAuth2::Error,

This is what my files look like

using gem figaro for handling the keys.

application.yml:

STRIPE_SECRET: sk_test_*************** STRIPE_CONNECT_CLIENT_ID: ca_**************

omniauth.rb:

Rails.application.config.middleware.use OmniAuth::Builder do provider :stripe_connect, ENV['STRIPE_CONNECT_CLIENT_ID'], ENV['STRIPE_SECRET'] end

secrets.yml:

STRIPE_SECRET: sk_test_*************** STRIPE_CONNECT_CLIENT_ID: ca_**************



via Chebli Mohamed

Compare sql VS active record requests with Benchmark-ips

I'm trying to compare active record request with sql request in my rails project. I'm using Benchmark-ips and pry.

Here what's happen :

[4] pry(main)> Benchmark.ips do |x|
[4] pry(main)*   x.report("sql request") { User.where("EXISTS (SELECT 1 FROM groups_users WHERE groups_users.user_id = users.id AND groups_users.group_id IN (?))", [8939, 8950]).where("NOT EXISTS (SELECT 1 FROM groups_users WHERE groups_users.user_id = users.id AND groups_users.group_id IN (?))", [8942]).ids }
[4] pry(main)*   x.report("active record") { (User.joins(:groups).where(groups: {id: ["8939","8950"]}) - User.joins(:groups).where(groups: {id: 8942})).map(&:id) }
[4] pry(main)*   x.compare!
[4] pry(main)* end
Calculating -------------------------------------
         sql requestNameError: uninitialized constant User
from (pry):15:in `block (2 levels) in __pry__'

I did the same with the classic benchmark tool.

[65] pry(main)> Benchmark.bm do |x|
[65] pry(main)*   x.report("sql request") { User.where("EXISTS (SELECT 1 FROM groups_users WHERE groups_users.user_id = users.id AND groups_users.group_id IN (?))", [8939, 8950]).where("NOT EXISTS (SELECT 1 FROM groups_users WHERE groups_users.user_id = users.id AND groups_users.group_id IN (?))", [8942]).ids }
[65] pry(main)*   x.report("active record") { (User.joins(:groups).where(groups: {id: ["8939","8950"]}) - User.joins(:groups).where(groups: {id: 8942})).map(&:id) }
[65] pry(main)* end
       user     system      total        real
sql request   (1.5ms)  SELECT "users"."id" FROM "users" WHERE (EXISTS (SELECT 1 FROM groups_users WHERE groups_users.user_id = users.id AND groups_users.group_id IN (8939,8950))) AND (NOT EXISTS (SELECT 1 FROM groups_users WHERE groups_users.user_id = users.id AND groups_users.group_id IN (8942)))
  0.010000   0.010000   0.020000 (  0.027799)
active record  User Load (9.4ms)  SELECT "users".* FROM "users" INNER JOIN "groups_users" ON "groups_users"."user_id" = "users"."id" INNER JOIN "groups" ON "groups"."id" = "groups_users"."group_id" WHERE "groups"."id" IN (8939, 8950)
  User Load (0.8ms)  SELECT "users".* FROM "users" INNER JOIN "groups_users" ON "groups_users"."user_id" = "users"."id" INNER JOIN "groups" ON "groups"."id" = "groups_users"."group_id" WHERE "groups"."id" = $1  [["id", 8942]]
  0.050000   0.000000   0.050000 (  0.074597)
=> [#<Benchmark::Tms:0x007ff477ce7c28 @cstime=0.0, @cutime=0.0, @label="sql request", @real=0.02779875499982154, @stime=0.010000000000000009, @total=0.02000000000000024, @utime=0.010000000000000231>,
 #<Benchmark::Tms:0x007ff473f3f7e0 @cstime=0.0, @cutime=0.0, @label="active record", @real=0.07459704099892406, @stime=0.0, @total=0.04999999999999982, @utime=0.04999999999999982>]



via Chebli Mohamed

restoring bundle path to default after accidentally erasing it

I'm running a Mac, using Terminal, working with Ruby on Rails.

I was trying to figure out where bundle install gemfiles on my machine. So I ran the command

bundle --path

and now it appears my machine does not know where any of my gem files are located. How do I restore it to it's original functionality?

Here is the error...

Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

/Users/theDanOtto/.rvm/rubies/ruby-2.1.2/bin/ruby extconf.rb
/Users/theDanOtto/.rvm/rubies/ruby-2.1.2/bin/ruby: invalid option -D  (-h will show valid options) (RuntimeError)

extconf failed, exit code 1

Gem files will remain installed in /Users/theDanOtto/Dropbox/Sites/Current Development/ClashOfClanV2/path/ruby/2.1.0/gems/json-1.8.3 for inspection.



via Chebli Mohamed

Rails Migration: How to increase INTEGER size by using ROR migration

My contacts table number column is Integer type. Now I am planning to increase the limit.

Any one please let me know how can we increase limit by using ROR migration.



via Chebli Mohamed

Undefined method `name' for nil:NilClass for search form

I am creating search form, and I have created search controller and other controller named as user. Here is the code of search_controller

def search
  if params[:q]
    @users = User.q(params[:q]).order("created_at DESC").paginate(page: params[:page])
  else
    @users = User.all.order('created_at DESC').paginate(page: params[:page])
  end
end

Here is the search form

<%= form_tag search_path, :method => 'get' do %>
  <%= text_field_tag :q, params[:q] %>
  <%= submit_tag "Search", :name => nil %>
<% end %>

But when I search I am getting undefined method 'name' for nil:NilClass error.

class UsersController < ApplicationController
  before_action :logged_in_user, only: [:index, :edit, :destroy] 

  def index
    @users = User.paginate(page: params[:page])
  end

  def show
    @user = User.find(params[:id])
    @archings = @user.archings.paginate(page: params[:page])
  end

  def new
    @user = User.new
  end

  def destroy
    User.find(params[:id]).destroy
    flash[:success] = "User deleted"
    redirect_to users_url
  end

  def create
    @user = User.new(user_params)
    if @user.save
      log_in @user
      flash[:success] = "Welcome to the Arch"
      redirect_to @user
    else
      render 'new'
    end
  end

  def edit
    @user = User.find(params[:id])
  end

  private

    def user_params
      params.require(:user).permit(:name, :email, :password,
                                   :password_confirmation)
    end

   def logged_in_user
      unless logged_in?
    store_location
        flash[:danger] = "Please log in."
        redirect_to login_url
      end

 end
end

Can anyone tell me where I am doing mistake?

PS: I am beginner and new to rails and ruby.



via Chebli Mohamed

Paperclip plugin restrict image upload types also allow image to be null. Rails

In my Rails 2 application, images for products are uploaded using Paperclip as a plugin. I need to restrict the image types to jpeg and png and also allow saving of product even if image is not uploaded.

The current code is

has_attached_file :master_image,
  :url  => "/images/products/:id/private/master.img",
  :path => ":rails_root/public/images/products/:id/private/master.img"

validates_attachment_content_type :master_image, :content_type => ['image/png', 'image/jpg'] , :message => "image must be jpg or png." , :allow_nil => true

I added :allow_nil => true but it is not working.

I am getting image must be jpg or png when trying to save without image.

Any help????



via Chebli Mohamed

Fetch bookmark from the database on the basis of tag

I am new in Rails, I have 3 models in the application namely Bookmark, Tag, Tagging. I want to fetch all the bookmark on the basis of tag. I have written the sql queries but i don't know how to implement these in rails.

Attributes of the model are:

Bookmark: id , name

Tag : id , name

Tagging : bookmark_id , tag_id

SQL Queries that i have written for that is :

select * from Bookmark where id = (select bookmark_id from tagging where tag_id = (select * from tag where name= 'tag1'))



via Chebli Mohamed