Sunday, April 3, 2016

Ruby On Rails - rendering custom data using devise_token_auth | Fixed issues

Ruby On Rails - rendering custom data using devise_token_auth | Fixed issues


Ruby On Rails - rendering custom data using devise_token_auth

Posted: 03 Apr 2016 06:56 AM PDT

I am currently using devise_token_auth to implement secure token based authentication for a my Rails API. This gem generates a User model with some attributes. After adding some custom attributes to my User model, the user management(log in, log out...) routes, provided by devise_token_auth, keep on rendering the same old attributes.

I've tried adding a UserSerializer but it didn't solve the issue.

Does anyone know how to render custom data for User model, using devise_token_auth?

Tell me the best way to build embedded mail client for CRM application (ruby on rails) . ( Inbox, Sent Items , basket ) [on hold]

Posted: 03 Apr 2016 06:54 AM PDT

Tell me the best way to build embedded mail client for CRM application (ruby on rails) . ( Inbox, Sent Items .. )

Parse xml file with nokogiri

Posted: 03 Apr 2016 06:52 AM PDT

I need to parse a xml file on ruby on rails, i'm using nokogiri gem to parse it.

I can parse like this to appear the parent and his children, but its appearing like this:

PARENT: Example Parent 1    CHILD: Example Children 1Example Children 2Example Children 3    PARENT: Example Parent 2    CHILD:  

It's missing the children of the second parent node. I did this like this:

In the controller:

  @codes = []      doc.xpath('//Node').each do |parent|          @parentN =parent.xpath('///ancestor::*/@name')            @codes << parent.xpath('Node/@name').text        end  

And the view:

<% for x in 0...@parentN.count %>        <p> PARENT: <%= @parentN[x]  %>  </p>      <p> CHILD:  <%= @codes[x] %>  </p>        <%   end %>  

How can I "connect" the parent with the childs? Presenting the parent and his children, and then other parent and childrens...

This is my xml file:

   <Report>         <Node name="Example Parent 1" color="red">            <Node name="Example Children 1" color="red" rank="very high" />            <Node name="Example Children 2" color="red" rank="high" />            <Node name="Example Children 3" color="yellow" rank="moderate" />         </Node>         <Node name="Example Parent 2" color="yellow">            <Node name="Example Children 1" color="yellow" rank="moderate" />         </Node>      </Report>  

Google maps not showing when clicking on href link

Posted: 03 Apr 2016 05:49 AM PDT

I have integrated google maps in one of my web app page(Say, pageA). Web app is implemented in RoR and I am working on localhost/development phase for now.

Interesting thing is that google map does not display correctly when I reach to pageA by clicking on some href link but it displays correctly when I refresh the page(pageA)

I do not know in which direction I should start by debugging. Is it browser or cache or something else?

I hope that code part does not have anything to do with this problem since it is opening while refreshing the page. Having said that, I would be happy to share the code part in case someone want to have a look at it.

Postgres issue - Ruby on Rails (Postgres) on Google Container Engine

Posted: 03 Apr 2016 05:46 AM PDT

Run into a little problem and I'm hoping someone can point me in the right direction. Im running a Rails+Postgres multi-container and they start up fine, except rails shows this in the logs when I try access the IP of the LoadBalancer:

PG::ConnectionBad (could not connect to server: No such file or directory  Is the server running locally and accepting connections on  Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?):  

My two container pod files and my database.yml are as follows:

RAILS POD

apiVersion: v1  kind: Pod  metadata:    name: cartelhouse    labels:      name: cartelhouse  spec:    containers:      - image: gcr.io/xyz/cartelhouse:v6        name: cartelhouse        env:          - name: POSTGRES_PASSWORD            # Change this - must match postgres.yaml password.            value: mypassword          - name: POSTGRES_USER            value: rails        ports:          - containerPort: 80            name: cartelhouse        volumeMounts:            # Name must match the volume name below.          - name: cartelhouse-persistent-storage            # Mount path within the container.            mountPath: /var/www/html    volumes:      - name: cartelhouse-persistent-storage        gcePersistentDisk:          # This GCE persistent disk must already exist.          pdName: rails-disk          fsType: ext4  ​  ​  

POSTGRES POD

apiVersion: v1  kind: Pod  metadata:    name: postgres    labels:      name: postgres  spec:    containers:      - name: postgres        image: postgres        env:          - name: POSTGRES_PASSWORD            value: mypassword          - name: POSTGRES_USER            value: rails          - name: PGDATA            value: /var/lib/postgresql/data/pgdata        ports:          - containerPort: 5432            name: postgres                volumeMounts:          - name: postgres-persistent-storage            mountPath: /var/lib/postgresql/data    volumes:      - name: postgres-persistent-storage        gcePersistentDisk:          # This disk must already exist.          pdName: postgres-disk          fsType: ext4  ​  

DATABASE.YML FILE

​  production:    <<: *default    adapter: postgresql    encoding: unicode    database: app_production    username: <%= ENV['PG_ENV_POSTGRES_USER'] %>    password: <%= ENV['PG_ENV_POSTGRES_PASSWORD'] %>    host:     <%= ENV['PG_PORT_5432_TCP_ADDR'] %>  

I assume its a linking issue, or something to do with PGDATA paths I specified?

Ruby on Rails has_many :through Association controller

Posted: 03 Apr 2016 05:54 AM PDT

these are my 3 models :

model for User:

class User < ActiveRecord::Base  has_many :patients, through: :treatments  has_many :treatments   .  .  .  

model for patient:

class Patient < ActiveRecord::Base  has_many :user, through: :treatments   has_many :treatments, dependent: :destroy  .  .  .  

model for treatment:

class Treatment < ActiveRecord::Base  belongs_to :patient  belongs_to :user  validates :patient_id, presence: true  default_scope -> { order(created_at: :desc) }  end  

And this is my treatment table :

  class CreateTreatments < ActiveRecord::Migration     def change       create_table :treatments do |t|        t.date :teartment_date        t.text :remark        t.float :fee        t.references :patient, index: true, foreign_key: true          t.timestamps null: false      end     add_index :treatments, [:patient_id, :created_at]   end  end  

now i want to define a controller to create a new treatment that belongs to a specific user's patient.

this is my controller :

  def new      @treat = Treatment.new    end      def create           @userpatient = current_user.treatments.build(treat_params)        if @userpatient.save      flash[:success] = "new treatment added"      redirect_to root_url    else      render 'new'     end  end  

but this is the error that i receive, while i want to create a new treatment :

ActiveRecord::UnknownAttributeError in TreatmentsController#create     unknown attribute 'user_id' for Treatment.  

and this is the current_user :

 def current_user    if (user_id = session[:user_id])     @current_user ||= User.find_by(id: user_id)    elsif (user_id = cookies.signed[:user_id])     user = User.find_by(id: user_id)       if user && user.authenticated?(cookies[:remember_token])       log_in user       @current_user = user     end     end     end  

i'm new to rails, the basic idea is i want my user to have treatment that belongs to a specific patient.

Thanks to replies i've over come with this issue by adding a reference column . now i receive no, but it does not save any treatments. i mean the part :

if @treat.save    flash[:success] = "new treatment added"    redirect_to root_url  else    render 'new'  end  

it does not save and just render 'new' .

i have 2 questions :

1- how can i code my create controller ?

2- how to retrieve my treatments base on patient.what variable should i define in my patient 'show' method to have its treatments retrieved ?

Deploying multiple Rails applications to DigitalOcean

Posted: 03 Apr 2016 05:02 AM PDT

I'm using Digital Ocean droplet for rails application. I've managed to deploy first application with success, but now facing with problems trying to deploy the second one. I am using unicorn as app server and nginx as web server. OS is Ubuntu 14.04

I've read lots of threads on stackexchange sites, also on blogs etc. but none of them fits my position. Problem is, I think, on app and system folder/file/configuration structures. Which I am very cautious to change anything on system configuration files.

In most examples on web, everyone is talking about unicorn.rb inside rails_root/config/ however I don't have any. Instead I have unicorn.conf with same content inside /etc.

There is also a socket file which listens for first app, and I tried two create the second for my second app - but it failed.

I know, I have to create another unicorn configuration for second app, and also have to do something which should be resulted with the creation of a socket for second.

But the lack of knowledge and understanding about system administration drives me to trouble.

Can anyone guide me about this problem?

I can provide more files if needed.

nginx configuration file for first app (path /etc/sites-available/first_app).

upstream app_server {      server unix:/var/run/unicorn.sock fail_timeout=0;  }    server {      listen   80;      root /home/rails/myfirstapp/public;      server_name www.myfirstapp.com;      index index.htm index.html index.php index.asp index.aspx index.cgi index.pl index.jsp;        location / {              try_files $uri/index.html $uri.html $uri @app;      }          location ~* ^.+\.(jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|mp3|flv|mpeg|avi)$ {                      try_files $uri @app;              }         location @app {              proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;              proxy_set_header Host $http_host;              proxy_redirect off;              proxy_pass http://app_server;      }  }    server {      listen 80;      server_name www.myfirstapp.com;      return 301 $scheme://myfirstapp.com$request_uri;      }  

second app (/etc/sites-available/second_app)

upstream app_server_2 {      server unix:/var/run/unicorn.app_two.sock fail_timeout=0;  }    server {      listen   80;      root /home/rails/secondapp/public;      server_name secondapp.com;      index index.htm index.html index.php index.asp index.aspx index.cgi index.pl index.jsp;        location / {              try_files $uri/index.html $uri.html $uri @app;      }          location ~* ^.+\.(jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|mp3|flv|mpeg|avi)$ {                      try_files $uri @app;              }         location @app {              proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;              proxy_set_header Host $http_host;              proxy_redirect off;              proxy_pass http://app_server_2;      }  }    server {      listen 80;      server_name secondapp.com www.secondapp.com;      return 301 $scheme://secondapp.com$request_uri;   }  

(/etc/unicorn.conf)

listen "unix:/var/run/unicorn.sock"  worker_processes 4  user "rails"  working_directory "/home/rails/myfirstapp"  pid "/var/run/unicorn.pid"  stderr_path "/var/log/unicorn/unicorn.log"  stdout_path "/var/log/unicorn/unicorn.log"  

How to get response from Sidekiq Worker?

Posted: 03 Apr 2016 05:01 AM PDT

I am new to Sidekiq background job world. I have successfully setup Sidekiq with redis and it's working fine. I am wondering how to get response from Sidekiq Job or worker when it will complete ? Actually I want to display all successful jobs with some useful info about it like it's arguments. Is possible to do this without any extension gem?

Extras ::

  • I want to trigger email when job will complete

Thanks in advance :-)

how should i manage videos in the backend?

Posted: 03 Apr 2016 04:40 AM PDT

I'm building a platform to to send through the API clips of videos you made with your mobile (back and forward). I know python, javascript and ruby, and now i have a dilemma. which framework should I use?

I'm not asking which one is the best (express, rails or django). I had to figure out the best, fastest and the most secure way to send videos to my backend and back. My db is a noSQL (mongodb)

  • Rails is solid with the APIs but have some "problems" in async
  • Django is good with images but I have no idea how it can manage videos
  • Express or similar are fast to read/write mongodb but it is a pain in the neck in terms of developing fast and you had to create a manual admin

Rails form - dynamic value of the hint

Posted: 03 Apr 2016 04:38 AM PDT

While I am generating form, one of my input field's hint has to list values that had been already taken by other records. I can not find any reference how to make such hint with dynamic value.

Here is what I am trying to do:

f.input :code, :as => :number, placeholder: '00', hint:     Scenario.select(:code).distinct.each do |c|    c.code.to_s  end  

What am I missing here?

Why am I unable to install Rails on Windows command line

Posted: 03 Apr 2016 04:24 AM PDT

After running the Rails installer on my Windows 7 laptop, the command line with directory set to C:\Sites appears. I try to run the command below

gem install rails  

But I get the error below:

Error executing gem ...

(Errno::EMSGSIZE) A message sent on a datagram socket was larger than the internal message buffer or some other network limit, or the buffer used to receive a datagram into was smaller than the datagram itself. - recvfrom(2)

Here is screen shot: Error on rails install

How do I fix this?

Thanks in advance

God/Resque Gemfile path not updated: Bundler::GemfileNotFound

Posted: 03 Apr 2016 04:00 AM PDT

I am using God for monitoring resque process and capistrano for application deployment.

My resque.god.rb looks like this:

rails_root  = ENV['RAILS_ROOT'] || File.dirname(__FILE__) + '/..'  rails_env   = ENV['RAILS_ENV'] or raise 'RAILS_ENV not set'  queue_check_interval = 3  num_workers = 1  queue = ['requester']    num_workers.times do |num|    God.watch do |w|      w.name     = "resque_#{rails_env}_#{num}"      w.group    = 'resque'      w.log      = "#{rails_root}/log/god.log"      w.interval = 30.seconds      w.env      = {'QUEUE'=>queue[num], 'RAILS_ENV'=>rails_env}      w.start    = "rake -f #{rails_root}/Rakefile environment INTERVAL=#{queue_check_interval} resque:work"        # restart if memory gets too high      w.transition(:up, :restart) do |on|        on.condition(:memory_usage) do |c|          c.above = 300.megabytes          c.times = 2        end      end        # determine the state on startup      w.transition(:init, { true => :up, false => :start }) do |on|        on.condition(:process_running) do |c|          c.running = true        end      end        # determine when process has finished starting      w.transition([:start, :restart], :up) do |on|        on.condition(:process_running) do |c|          c.running = true          c.interval = 5.seconds        end          # failsafe        on.condition(:tries) do |c|          c.times = 5          c.transition = :start          c.interval = 5.seconds        end      end        # start if process is not running      w.transition(:up, :start) do |on|        on.condition(:process_running) do |c|          c.running = false        end      end    end  end  

Whenever I am trying to restart the process, I get the following error:

/home/stage/.rvm/gems/ruby-2.3.0/gems/bundler-1.11.2/lib/bundler/definition.rb:22:in `build': /home/stage/app/project/releases/20160401213805/Gemfile not found (Bundler::GemfileNotFound)    from /home/stage/.rvm/gems/ruby-2.3.0/gems/bundler-1.11.2/lib/bundler.rb:120:in `definition'    from /home/stage/.rvm/gems/ruby-2.3.0/gems/bundler-1.11.2/lib/bundler.rb:88:in `setup'    from /home/stage/.rvm/gems/ruby-2.3.0/gems/bundler-1.11.2/lib/bundler/setup.rb:18:in `<top (required)>'    from /home/stage/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'    from /home/stage/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'  

clearly the gemfile path is not updated since it is looking for the gemfile in a past release which doesn't exist anymore since it has been removed by capistrano.

How do I solve this issue?

Rails routes: Nested, Member, Collection, namespace, scope and customizable

Posted: 03 Apr 2016 03:37 AM PDT

I am trying to understand more about Rails routes.

Member and Collection

  # Example resource route with options:    #   resources :products do    #     member do    #       get 'short'    #       post 'toggle'    #     end    #    #     collection do    #       get 'sold'    #     end    #   end  

Namespace and Scope

  # Example resource route within a namespace:    #   namespace :admin do    #     resources :products    #   end      #   scope :admin do    #     resources :products    #   end  

Constraints, Redirect_to

# Example resource route with options:  # get "/questions", to: redirect {|params, req|   #     begin  #       id = req.params[:category_id]  #       cat = Category.find(id)  #       "/abc/#{cat.slug}"  #     rescue  #       "/questions"  #     end  # }  

Customization:

resource :profiles  

original url from resource profiles for edit.

http://localhost:3000/profiles/1/edit  

I want to make it for users available only through click edit profile and see url like in below.

http://localhost:3000/profile/edit  

Also, is there advanced routing, How most big companies design their routes in rails ? I would be really glad to see new kind of routes if there exist.

Thank You !

Accessing the owner object in mongoid association

Posted: 03 Apr 2016 03:17 AM PDT

ActiveRecord has such feature

class User < ActiveRecord::Base    has_many :birthday_events, ->(user) { where starts_on: user.birthday}, class_name: 'Event'  end  

But when I try to use it in mongoid association I get error: "no implicit conversion of Proc into Hash"

So is there any alternative of this feature in mongoid?

Rails: Including nested associative attributes in response

Posted: 03 Apr 2016 03:23 AM PDT

I have three models as follows :

 #Product Model   class Product < ActiveRecord::Base          belongs_to :user          has_one :address          validates :title, :description, :user_id, presence: true          validates :product_type, numericality:{:greater_than => 0, :less_than_or_equal_to => 2}, presence: true          accepts_nested_attributes_for :address      end              #Address Model      class Address < ActiveRecord::Base          belongs_to :city          belongs_to :product            def related_city              city = address.city          end      end    #City Model  class City < ActiveRecord::Base      has_many :addresses  end  

I am fetching a Product but I need to include associative attributes as well in my JSON response except few attributes. Here is what I have done so far :

def show          product = Product.find(params[:id])          render json: product.to_json(:include => { :address => {                               :include => { :city => {                                               :only => :name } },                               },:user =>{:only=>{:first_name}}}), status: 200      end  

This is giving me a syntax error. If I remove the user it is working fine but I need user's name as well in response. Moreover how would I write the above code using ruby's new hash syntax?

How to check rails compilation before deployment

Posted: 03 Apr 2016 03:03 AM PDT

I encounter sometimes a broken code that if I run rails c/s locally it doesn't break. Only after deployment (RAILS_ENV = production) it will break on rails server loading...

Before deployment I do RAILS_ENV=production rails s to see if it breaks or not.

Is there a way to test this differently?

Thanks

Rails polymorphic join table work both ways

Posted: 03 Apr 2016 05:09 AM PDT

I am starting to learn more advanced associations, and polymorphic join table looks very interesting, but I've come to a bad limitation.

My models:

school_class.rb:

class SchoolClass < ActiveRecord::Base    has_many :class_of_schools, as: :school_model, dependent: :destroy    has_many :basic_primary_schools, through: :class_of_schools    has_many :basic_shugenja_schools, through: :class_of_schools    has_many :basic_monk_schools, through: :class_of_schools  end  

class_of_school.rb:

class ClassOfSchool < ActiveRecord::Base    belongs_to :school_model, polymorphic: true    belongs_to :school_class  end  

basic_primary_School.rb:

class BasicPrimarySchool < ActiveRecord::Base      has_many :class_of_schools, as: :school_model, dependent: :destroy    has_many :school_classes, through: :class_of_schools  end  

shugenja_school and monk_schools, has the same association as basic_primary_school.

And join model it self:

class CreateClassOfSchools < ActiveRecord::Migration    def change      create_table :class_of_schools do |t|        t.integer :school_class_id        t.integer :school_model_id        t.string :school_model_type          t.timestamps null: false      end    end  end  

It joins well on the school side, I can make school.class_schools, and I get the array of school_classes associated. But on the other side I can`t do the same. In fact when I check school_class.classes_of schools I get empty array. I make associations in my seed file by function like this:

def join_schools_with_classes(school_object_name, school_classes_array)    school_object_name.all.each do |school|      school_classes = school_classes_array[school.name]      school_classes.each do |class_name|        school_class = SchoolClass.find_by(name: class_name)        school.class_of_schools.create( school_class_id: school_class.id)      end    end  end  

My question:

How can I make this association works both ways? So I can call ClassSchool.first.class_of_schools returns, all objects associated to this object. And still be able to call BasicPrimarySchool.first.school_classes to get associated school_class objects.

Increment @instance_variable in controller each time

Posted: 03 Apr 2016 03:52 AM PDT

Each time user clicks a link, I want to display him the next portion of data. I'm using an instance variable @i for that purpose:

# link in a view  <%= link_to 'Next', user_test_do_path, controller: :do, action: :list_update, user_id: 1, test_id: 2, remote: :true, HERE_WANT_TO_UPDATE_@i %>    # controller and action  class DoController < ApplicationController            def list_update      @i+=1      @cur_test = Test.where(id: params[:test_id])      @cur_q_all = Question.where(test_id: @cur_test)      @cur_q = @cur_q_all[@i]       @cur_ans_all = Answer.where(question_id: @cur_q)    end  end  

However, @i is not incremented and always stays equal to 0. My question is: how to make value of @i incremented with each click on a link_to ?

SQL query to extract all friends

Posted: 03 Apr 2016 02:36 AM PDT

I have this Rails models structure:

class User < ActiveModel::Base    has_many :posts    has_many :followers, foreign_key: :target_id, dependent: :destroy    has_many :following, class_name: 'Follower', foreign_key: :user_id, dependent: :destroy    has_many :comments  end    class Post < ActiveModel::Base    belongs_to :user    has_many: comments  end    class Comment < ActiveModel::Base    belongs_to :user    belongs_to :post  end    class Follower < ActiveModel::Base      belongs_to :user      belongs_to :target, class_name: 'User'  end  

Now, I need an SQL query that could extract for each post a list of all following users that commented on that post.

Rails Browserify React ExecJS error during precompile

Posted: 03 Apr 2016 12:52 AM PDT

I've tried out literally all the solutions that are available out there. But I'm stuck with this for the past 24 hours. I am not able to track where the error is.

Here's the error log which I got from rake assets:precompile --trace

ExecJS::ProgramError: Unexpected token punc «(», expected punc «:» (line: 38085, col: 18, pos: 1133749)    Error      at new JS_Parse_Error (<eval>:3572:11870)      at js_error (<eval>:3572:12089)      at croak (<eval>:3572:20898)      at token_error (<eval>:3572:21035)      at expect_token (<eval>:3572:21258)      at expect (<eval>:3572:21396)      at <eval>:3572:30647      at <eval>:3572:21788      at expr_atom (<eval>:3572:29403)      at maybe_unary (<eval>:3573:143)      at expr_ops (<eval>:3573:901)      at maybe_conditional (<eval>:3573:993)      at maybe_assign (<eval>:3573:1436)  new JS_Parse_Error ((execjs):3572:11870)  js_error ((execjs):3572:12089)  croak ((execjs):3572:20898)  token_error ((execjs):3572:21035)  expect_token ((execjs):3572:21258)  expect ((execjs):3572:21396)  (execjs):3572:30647  (execjs):3572:21788  expr_atom ((execjs):3572:29403)  maybe_unary ((execjs):3573:143)  expr_ops ((execjs):3573:901)  maybe_conditional ((execjs):3573:993)  maybe_assign ((execjs):3573:1436)  /Users/karthik/.rvm/gems/ruby-2.3.0/gems/execjs-2.6.0/lib/execjs/ruby_racer_runtime.rb:47:in `rescue in block in call'  /Users/karthik/.rvm/gems/ruby-2.3.0/gems/execjs-2.6.0/lib/execjs/ruby_racer_runtime.rb:44:in `block in call'  /Users/karthik/.rvm/gems/ruby-2.3.0/gems/execjs-2.6.0/lib/execjs/ruby_racer_runtime.rb:75:in `block in lock'  /Users/karthik/.rvm/gems/ruby-2.3.0/gems/execjs-2.6.0/lib/execjs/ruby_racer_runtime.rb:73:in `Locker'  /Users/karthik/.rvm/gems/ruby-2.3.0/gems/execjs-2.6.0/lib/execjs/ruby_racer_runtime.rb:73:in `lock'  /Users/karthik/.rvm/gems/ruby-2.3.0/gems/execjs-2.6.0/lib/execjs/ruby_racer_runtime.rb:43:in `call'  /Users/karthik/.rvm/gems/ruby-2.3.0/gems/uglifier-3.0.0/lib/uglifier.rb:176:in `run_uglifyjs'  /Users/karthik/.rvm/gems/ruby-2.3.0/gems/uglifier-3.0.0/lib/uglifier.rb:139:in `compile'  /Users/karthik/.rvm/gems/ruby-2.3.0/gems/sprockets-3.5.2/lib/sprockets/uglifier_compressor.rb:52:in `call'  /Users/karthik/.rvm/gems/ruby-2.3.0/gems/sprockets-3.5.2/lib/sprockets/uglifier_compressor.rb:28:in `call'  /Users/karthik/.rvm/gems/ruby-2.3.0/gems/sprockets-3.5.2/lib/sprockets/processor_utils.rb:75:in `call_processor'  /Users/karthik/.rvm/gems/ruby-2.3.0/gems/sprockets-3.5.2/lib/sprockets/processor_utils.rb:57:in `block in call_processors'  /Users/karthik/.rvm/gems/ruby-2.3.0/gems/sprockets-3.5.2/lib/sprockets/processor_utils.rb:56:in `reverse_each'  /Users/karthik/.rvm/gems/ruby-2.3.0/gems/sprockets-3.5.2/lib/sprockets/processor_utils.rb:56:in `call_processors'  /Users/karthik/.rvm/gems/ruby-2.3.0/gems/sprockets-3.5.2/lib/sprockets/loader.rb:134:in `load_from_unloaded'  /Users/karthik/.rvm/gems/ruby-2.3.0/gems/sprockets-3.5.2/lib/sprockets/loader.rb:60:in `block in load'  /Users/karthik/.rvm/gems/ruby-2.3.0/gems/sprockets-3.5.2/lib/sprockets/loader.rb:318:in `fetch_asset_from_dependency_cache'  /Users/karthik/.rvm/gems/ruby-2.3.0/gems/sprockets-3.5.2/lib/sprockets/loader.rb:44:in `load'  /Users/karthik/.rvm/gems/ruby-2.3.0/gems/sprockets-3.5.2/lib/sprockets/cached_environment.rb:20:in `block in initialize'  /Users/karthik/.rvm/gems/ruby-2.3.0/gems/sprockets-3.5.2/lib/sprockets/cached_environment.rb:47:in `load'  /Users/karthik/.rvm/gems/ruby-2.3.0/gems/sprockets-3.5.2/lib/sprockets/base.rb:66:in `find_asset'  /Users/karthik/.rvm/gems/ruby-2.3.0/gems/sprockets-3.5.2/lib/sprockets/base.rb:73:in `find_all_linked_assets'  /Users/karthik/.rvm/gems/ruby-2.3.0/gems/sprockets-3.5.2/lib/sprockets/manifest.rb:142:in `block in find'  /Users/karthik/.rvm/gems/ruby-2.3.0/gems/sprockets-3.5.2/lib/sprockets/legacy.rb:114:in `block (2 levels) in logical_paths'  /Users/karthik/.rvm/gems/ruby-2.3.0/gems/sprockets-3.5.2/lib/sprockets/path_utils.rb:225:in `block in stat_tree'  /Users/karthik/.rvm/gems/ruby-2.3.0/gems/sprockets-3.5.2/lib/sprockets/path_utils.rb:209:in `block in stat_directory'  /Users/karthik/.rvm/gems/ruby-2.3.0/gems/sprockets-3.5.2/lib/sprockets/path_utils.rb:206:in `each'  /Users/karthik/.rvm/gems/ruby-2.3.0/gems/sprockets-3.5.2/lib/sprockets/path_utils.rb:206:in `stat_directory'  /Users/karthik/.rvm/gems/ruby-2.3.0/gems/sprockets-3.5.2/lib/sprockets/path_utils.rb:224:in `stat_tree'  /Users/karthik/.rvm/gems/ruby-2.3.0/gems/sprockets-3.5.2/lib/sprockets/legacy.rb:105:in `each'  /Users/karthik/.rvm/gems/ruby-2.3.0/gems/sprockets-3.5.2/lib/sprockets/legacy.rb:105:in `block in logical_paths'  /Users/karthik/.rvm/gems/ruby-2.3.0/gems/sprockets-3.5.2/lib/sprockets/legacy.rb:104:in `each'  /Users/karthik/.rvm/gems/ruby-2.3.0/gems/sprockets-3.5.2/lib/sprockets/legacy.rb:104:in `logical_paths'  /Users/karthik/.rvm/gems/ruby-2.3.0/gems/sprockets-3.5.2/lib/sprockets/manifest.rb:140:in `find'  /Users/karthik/.rvm/gems/ruby-2.3.0/gems/sprockets-3.5.2/lib/sprockets/manifest.rb:168:in `compile'  /Users/karthik/.rvm/gems/ruby-2.3.0/gems/sprockets-rails-3.0.4/lib/sprockets/rails/task.rb:68:in `block (3 levels) in define'  /Users/karthik/.rvm/gems/ruby-2.3.0/gems/sprockets-3.5.2/lib/rake/sprocketstask.rb:147:in `with_logger'  /Users/karthik/.rvm/gems/ruby-2.3.0/gems/sprockets-rails-3.0.4/lib/sprockets/rails/task.rb:67:in `block (2 levels) in define'  /Users/karthik/.rvm/gems/ruby-2.3.0/gems/rake-11.1.2/lib/rake/task.rb:248:in `block in execute'  /Users/karthik/.rvm/gems/ruby-2.3.0/gems/rake-11.1.2/lib/rake/task.rb:243:in `each'  /Users/karthik/.rvm/gems/ruby-2.3.0/gems/rake-11.1.2/lib/rake/task.rb:243:in `execute'  /Users/karthik/.rvm/gems/ruby-2.3.0/gems/rake-11.1.2/lib/rake/task.rb:187:in `block in invoke_with_call_chain'  /Users/karthik/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/monitor.rb:214:in `mon_synchronize'  /Users/karthik/.rvm/gems/ruby-2.3.0/gems/rake-11.1.2/lib/rake/task.rb:180:in `invoke_with_call_chain'  /Users/karthik/.rvm/gems/ruby-2.3.0/gems/rake-11.1.2/lib/rake/task.rb:173:in `invoke'  /Users/karthik/.rvm/gems/ruby-2.3.0/gems/rake-11.1.2/lib/rake/application.rb:150:in `invoke_task'  /Users/karthik/.rvm/gems/ruby-2.3.0/gems/rake-11.1.2/lib/rake/application.rb:106:in `block (2 levels) in top_level'  /Users/karthik/.rvm/gems/ruby-2.3.0/gems/rake-11.1.2/lib/rake/application.rb:106:in `each'  /Users/karthik/.rvm/gems/ruby-2.3.0/gems/rake-11.1.2/lib/rake/application.rb:106:in `block in top_level'  /Users/karthik/.rvm/gems/ruby-2.3.0/gems/rake-11.1.2/lib/rake/application.rb:115:in `run_with_threads'  /Users/karthik/.rvm/gems/ruby-2.3.0/gems/rake-11.1.2/lib/rake/application.rb:100:in `top_level'  /Users/karthik/.rvm/gems/ruby-2.3.0/gems/rake-11.1.2/lib/rake/application.rb:78:in `block in run'  /Users/karthik/.rvm/gems/ruby-2.3.0/gems/rake-11.1.2/lib/rake/application.rb:176:in `standard_exception_handling'  /Users/karthik/.rvm/gems/ruby-2.3.0/gems/rake-11.1.2/lib/rake/application.rb:75:in `run'  /Users/karthik/.rvm/gems/ruby-2.3.0/gems/rake-11.1.2/bin/rake:33:in `<top (required)>'  /Users/karthik/.rvm/gems/ruby-2.3.0/bin/rake:23:in `load'  /Users/karthik/.rvm/gems/ruby-2.3.0/bin/rake:23:in `<main>'  /Users/karthik/.rvm/gems/ruby-2.3.0/bin/ruby_executable_hooks:15:in `eval'  /Users/karthik/.rvm/gems/ruby-2.3.0/bin/ruby_executable_hooks:15:in `<main>'  V8::Error: Unexpected token punc «(», expected punc «:»  at js_error (<eval>:3572:12089)  at croak (<eval>:3572:20898)  at token_error (<eval>:3572:21035)  at expect_token (<eval>:3572:21258)  at expect (<eval>:3572:21396)  at <eval>:3572:30647  at <eval>:3572:21788  at expr_atom (<eval>:3572:29403)  at maybe_unary (<eval>:3573:143)  at expr_ops (<eval>:3573:901)  at maybe_conditional (<eval>:3573:993)  at maybe_assign (<eval>:3573:1436)  at expression (<eval>:3573:1749)  at expr_list (<eval>:3572:29964)  at subscripts (<eval>:3572:31880)  at subscripts (<eval>:3572:31516)  at expr_atom (<eval>:3572:29621)  at maybe_unary (<eval>:3573:143)  at expr_ops (<eval>:3573:901)  at maybe_conditional (<eval>:3573:993)  at maybe_assign (<eval>:3573:1436)  at expression (<eval>:3573:1749)  at vardefs (<eval>:3572:27739)  at var_ (<eval>:3572:27894)  at <eval>:3572:23683  at <eval>:3572:21788  at block_ (<eval>:3572:26497)  at ctor.body (<eval>:3572:26131)  at function_ (<eval>:3572:26196)  at expr_atom (<eval>:3572:29484)  at maybe_unary (<eval>:3573:143)  at expr_ops (<eval>:3573:901)  at maybe_conditional (<eval>:3573:993)  at maybe_assign (<eval>:3573:1436)  at expression (<eval>:3573:1749)  at expr_list (<eval>:3572:29964)  at <eval>:3572:30078  at <eval>:3572:21788  at expr_atom (<eval>:3572:29355)  at maybe_unary (<eval>:3573:143)  at expr_ops (<eval>:3573:901)  at maybe_conditional (<eval>:3573:993)  at maybe_assign (<eval>:3573:1436)  at expression (<eval>:3573:1749)  at <eval>:3572:30733  at <eval>:3572:21788  at expr_atom (<eval>:3572:29403)  at maybe_unary (<eval>:3573:143)  at expr_ops (<eval>:3573:901)  at maybe_conditional (<eval>:3573:993)  at maybe_assign (<eval>:3573:1436)  at expression (<eval>:3573:1749)  at expr_list (<eval>:3572:29964)  at subscripts (<eval>:3572:31880)  at expr_atom (<eval>:3572:29302)  at maybe_unary (<eval>:3573:143)  at expr_ops (<eval>:3573:901)  at maybe_conditional (<eval>:3573:993)  at maybe_assign (<eval>:3573:1436)  at expression (<eval>:3573:1749)  at simple_statement (<eval>:3572:24461)  at <eval>:3572:22602  at <eval>:3572:21788  at <eval>:3573:2092  at parse (<eval>:3573:2301)  at parse (<eval>:3903:22)  at uglifier (<eval>:3946:13)  /Users/karthik/.rvm/gems/ruby-2.3.0/gems/execjs-2.6.0/lib/execjs/ruby_racer_runtime.rb:45:in `block in call'  /Users/karthik/.rvm/gems/ruby-2.3.0/gems/execjs-2.6.0/lib/execjs/ruby_racer_runtime.rb:75:in `block in lock'  /Users/karthik/.rvm/gems/ruby-2.3.0/gems/execjs-2.6.0/lib/execjs/ruby_racer_runtime.rb:73:in `Locker'  /Users/karthik/.rvm/gems/ruby-2.3.0/gems/execjs-2.6.0/lib/execjs/ruby_racer_runtime.rb:73:in `lock'  /Users/karthik/.rvm/gems/ruby-2.3.0/gems/execjs-2.6.0/lib/execjs/ruby_racer_runtime.rb:43:in `call'  /Users/karthik/.rvm/gems/ruby-2.3.0/gems/uglifier-3.0.0/lib/uglifier.rb:176:in `run_uglifyjs'  /Users/karthik/.rvm/gems/ruby-2.3.0/gems/uglifier-3.0.0/lib/uglifier.rb:139:in `compile'  /Users/karthik/.rvm/gems/ruby-2.3.0/gems/sprockets-3.5.2/lib/sprockets/uglifier_compressor.rb:52:in `call'  /Users/karthik/.rvm/gems/ruby-2.3.0/gems/sprockets-3.5.2/lib/sprockets/uglifier_compressor.rb:28:in `call'  /Users/karthik/.rvm/gems/ruby-2.3.0/gems/sprockets-3.5.2/lib/sprockets/processor_utils.rb:75:in `call_processor'  /Users/karthik/.rvm/gems/ruby-2.3.0/gems/sprockets-3.5.2/lib/sprockets/processor_utils.rb:57:in `block in call_processors'  /Users/karthik/.rvm/gems/ruby-2.3.0/gems/sprockets-3.5.2/lib/sprockets/processor_utils.rb:56:in `reverse_each'  /Users/karthik/.rvm/gems/ruby-2.3.0/gems/sprockets-3.5.2/lib/sprockets/processor_utils.rb:56:in `call_processors'  /Users/karthik/.rvm/gems/ruby-2.3.0/gems/sprockets-3.5.2/lib/sprockets/loader.rb:134:in `load_from_unloaded'  /Users/karthik/.rvm/gems/ruby-2.3.0/gems/sprockets-3.5.2/lib/sprockets/loader.rb:60:in `block in load'  /Users/karthik/.rvm/gems/ruby-2.3.0/gems/sprockets-3.5.2/lib/sprockets/loader.rb:318:in `fetch_asset_from_dependency_cache'  /Users/karthik/.rvm/gems/ruby-2.3.0/gems/sprockets-3.5.2/lib/sprockets/loader.rb:44:in `load'  /Users/karthik/.rvm/gems/ruby-2.3.0/gems/sprockets-3.5.2/lib/sprockets/cached_environment.rb:20:in `block in initialize'  /Users/karthik/.rvm/gems/ruby-2.3.0/gems/sprockets-3.5.2/lib/sprockets/cached_environment.rb:47:in `load'  /Users/karthik/.rvm/gems/ruby-2.3.0/gems/sprockets-3.5.2/lib/sprockets/base.rb:66:in `find_asset'  /Users/karthik/.rvm/gems/ruby-2.3.0/gems/sprockets-3.5.2/lib/sprockets/base.rb:73:in `find_all_linked_assets'  /Users/karthik/.rvm/gems/ruby-2.3.0/gems/sprockets-3.5.2/lib/sprockets/manifest.rb:142:in `block in find'  /Users/karthik/.rvm/gems/ruby-2.3.0/gems/sprockets-3.5.2/lib/sprockets/legacy.rb:114:in `block (2 levels) in logical_paths'  /Users/karthik/.rvm/gems/ruby-2.3.0/gems/sprockets-3.5.2/lib/sprockets/path_utils.rb:225:in `block in stat_tree'  /Users/karthik/.rvm/gems/ruby-2.3.0/gems/sprockets-3.5.2/lib/sprockets/path_utils.rb:209:in `block in stat_directory'  /Users/karthik/.rvm/gems/ruby-2.3.0/gems/sprockets-3.5.2/lib/sprockets/path_utils.rb:206:in `each'  /Users/karthik/.rvm/gems/ruby-2.3.0/gems/sprockets-3.5.2/lib/sprockets/path_utils.rb:206:in `stat_directory'  /Users/karthik/.rvm/gems/ruby-2.3.0/gems/sprockets-3.5.2/lib/sprockets/path_utils.rb:224:in `stat_tree'  /Users/karthik/.rvm/gems/ruby-2.3.0/gems/sprockets-3.5.2/lib/sprockets/legacy.rb:105:in `each'  /Users/karthik/.rvm/gems/ruby-2.3.0/gems/sprockets-3.5.2/lib/sprockets/legacy.rb:105:in `block in logical_paths'  /Users/karthik/.rvm/gems/ruby-2.3.0/gems/sprockets-3.5.2/lib/sprockets/legacy.rb:104:in `each'  /Users/karthik/.rvm/gems/ruby-2.3.0/gems/sprockets-3.5.2/lib/sprockets/legacy.rb:104:in `logical_paths'  /Users/karthik/.rvm/gems/ruby-2.3.0/gems/sprockets-3.5.2/lib/sprockets/manifest.rb:140:in `find'  /Users/karthik/.rvm/gems/ruby-2.3.0/gems/sprockets-3.5.2/lib/sprockets/manifest.rb:168:in `compile'  /Users/karthik/.rvm/gems/ruby-2.3.0/gems/sprockets-rails-3.0.4/lib/sprockets/rails/task.rb:68:in `block (3 levels) in define'  /Users/karthik/.rvm/gems/ruby-2.3.0/gems/sprockets-3.5.2/lib/rake/sprocketstask.rb:147:in `with_logger'  /Users/karthik/.rvm/gems/ruby-2.3.0/gems/sprockets-rails-3.0.4/lib/sprockets/rails/task.rb:67:in `block (2 levels) in define'  /Users/karthik/.rvm/gems/ruby-2.3.0/gems/rake-11.1.2/lib/rake/task.rb:248:in `block in execute'  /Users/karthik/.rvm/gems/ruby-2.3.0/gems/rake-11.1.2/lib/rake/task.rb:243:in `each'  /Users/karthik/.rvm/gems/ruby-2.3.0/gems/rake-11.1.2/lib/rake/task.rb:243:in `execute'  /Users/karthik/.rvm/gems/ruby-2.3.0/gems/rake-11.1.2/lib/rake/task.rb:187:in `block in invoke_with_call_chain'  /Users/karthik/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/monitor.rb:214:in `mon_synchronize'  /Users/karthik/.rvm/gems/ruby-2.3.0/gems/rake-11.1.2/lib/rake/task.rb:180:in `invoke_with_call_chain'  /Users/karthik/.rvm/gems/ruby-2.3.0/gems/rake-11.1.2/lib/rake/task.rb:173:in `invoke'  /Users/karthik/.rvm/gems/ruby-2.3.0/gems/rake-11.1.2/lib/rake/application.rb:150:in `invoke_task'  /Users/karthik/.rvm/gems/ruby-2.3.0/gems/rake-11.1.2/lib/rake/application.rb:106:in `block (2 levels) in top_level'  /Users/karthik/.rvm/gems/ruby-2.3.0/gems/rake-11.1.2/lib/rake/application.rb:106:in `each'  /Users/karthik/.rvm/gems/ruby-2.3.0/gems/rake-11.1.2/lib/rake/application.rb:106:in `block in top_level'  /Users/karthik/.rvm/gems/ruby-2.3.0/gems/rake-11.1.2/lib/rake/application.rb:115:in `run_with_threads'  /Users/karthik/.rvm/gems/ruby-2.3.0/gems/rake-11.1.2/lib/rake/application.rb:100:in `top_level'  /Users/karthik/.rvm/gems/ruby-2.3.0/gems/rake-11.1.2/lib/rake/application.rb:78:in `block in run'  /Users/karthik/.rvm/gems/ruby-2.3.0/gems/rake-11.1.2/lib/rake/application.rb:176:in `standard_exception_handling'  /Users/karthik/.rvm/gems/ruby-2.3.0/gems/rake-11.1.2/lib/rake/application.rb:75:in `run'  /Users/karthik/.rvm/gems/ruby-2.3.0/gems/rake-11.1.2/bin/rake:33:in `<top (required)>'  /Users/karthik/.rvm/gems/ruby-2.3.0/bin/rake:23:in `load'  /Users/karthik/.rvm/gems/ruby-2.3.0/bin/rake:23:in `<main>'  /Users/karthik/.rvm/gems/ruby-2.3.0/bin/ruby_executable_hooks:15:in `eval'  /Users/karthik/.rvm/gems/ruby-2.3.0/bin/ruby_executable_hooks:15:in `<main>'  Tasks: TOP => assets:precompile  

Here's the stack that I use:

  1. Rails
  2. ReactJS
  3. NPM
  4. Bower
  5. Browserify
  6. Reactify

Please help me out on this! Thanks in advance. Is there any way I can track in which file the error is?

NameError in Admin::ResidentsController#destroy Rails

Posted: 03 Apr 2016 01:51 AM PDT

I am getting name error when I am deleting a resident model object using active admin

I have resident model :

class Resident < ActiveRecord::Base    has_many :leaves,dependent: :delete_all  end  

And second one is leave model:

class Leave < ActiveRecord::Base    belongs_to :resident  end  

Giving me following error:

error that I am getting while deleting resident

Also rails misinterpreted leave name and changed it to leafe.. so I renamed Or refactored files : decorators/leafe_decorator.rb to decorators/leave_decorator.rb

similarly in decorator tests.

Now I again searched thru my whole code for Leafe Keyword but it is not there. And still getting that error . What should I do ?

ERROR: undefined method `comemnts' for

Posted: 03 Apr 2016 12:46 AM PDT

I follow guide.ruby.org , but some error I don't know how to solve it.

class List < ActiveRecord::Base      has_many :comments, dependent: :destroy  end  

`

class Comment < ActiveRecord::Base    belongs_to :list  end  

`

class CommentsController < ApplicationController    before_action :set_list      def index      @comments = @list.comments.order('created_at DESC')    end      def create      @comment = @list.comments.create(comment_params)      @comment.user_id = current_user.id        if @comment.save          respond_to do |format|              format.html { redirect_to list_path(@list)  }              format.js          end      else        flash[:alert] = 'Check the comment form, something went wrong.'        render root_path      end    end      private      def comment_params      params.require(:comment).permit(:content)    end      def set_list      @list = List.find(params[:list_id])    end    end  

`

# gem 'simple_form'  # gem 'foundation-rails'      <div class="comment-form">          <%= simple_form_for [@list, @list.comemnts.build] do |f| %>              <%= f.textarea :content, placeholder: 'add comment...',                                 class: "comment_content",                                 id: "comment_content_#{list.id}",                                 data: { list_id: "#{list.id}",                                 value: "#{list.comments.count}" } %>               <%=f.button :submit, 'New Comment', class: 'comment-submit-button' %>          <% end %>        </div>  

But I got error, when I step-to-step from guide, all is OK,here is the bug info:

undefined method `comemnts' for #

something wrong? Thanks answer me.

Handling circular dependency in rails while using threads

Posted: 03 Apr 2016 05:34 AM PDT

Here are five files added to my app (no database, no setup as such required):

lib/tasks/precomputation.rake

namespace :precomputation do    desc "This fetches data for precomputation"    task fetch_all: :environment do        Precomputation.precompute_all_data      # end    end  end  

app/models/precomputation.rb

class Precomputation    def self.precompute_all_data      ad_accounts = [1,2,3]      bgthread = BackgroundThread::BGThreadPool.new(1)      tasks = []      ad_accounts.each do |ad_account_id|        p = Proc.new do          begin            MongoPipeline::FbAdCampaignMongoPipeline.new(ad_account_id).fetch_all            false          ensure            GC.start          end        end        tasks << [p, "test #{ad_account_id}"]      end      bgthread.add_randomized_tasks(tasks)      bgthread.do_work    end  end  

app/models/mongo_pipeline.rb

module MongoPipeline    class Base      def initialize(ad_account_id)      end        def insert_data        puts 'inserting data'      end        def fetch_all        extract_data # Child Class defines this method        insert_data # Base class defines this method      end    end  end  

app/models/mongo_pipeline/fb_ad_campaign_mongo_pipeline.rb

module MongoPipeline    class FbAdCampaignMongoPipeline < MongoPipeline::Base      def extract_data        puts 'here is campaign data'      end    end  end  

and app/models/background_thread.rb

(NOTE: Alternative implementation using parallel gem and no background thread also collapses with similar error -: https://github.com/pratik60/circular_dependency_havoc/tree/parallel)

Error log

rake aborted!  Circular dependency detected while autoloading constant MongoPipeline  /Users/pratikbothra/.rvm/gems/ruby-2.2.2/gems/activesupport-4.2.4/lib/active_support/dependencies.rb:492:in `load_missing_constant'  /Users/pratikbothra/.rvm/gems/ruby-2.2.2/gems/activesupport-4.2.4/lib/active_support/dependencies.rb:184:in `const_missing'  /Users/pratikbothra/.rvm/gems/ruby-2.2.2/gems/activesupport-4.2.4/lib/active_support/dependencies.rb:526:in `load_missing_constant'  /Users/pratikbothra/.rvm/gems/ruby-2.2.2/gems/activesupport-4.2.4/lib/active_support/dependencies.rb:184:in `const_missing'  /webapps/circular_dependency_havoc/app/models/precomputation.rb:9:in `block (2 levels) in precompute_all_data'  /webapps/circular_dependency_havoc/app/models/background_thread.rb:91:in `call'  /webapps/circular_dependency_havoc/app/models/background_thread.rb:91:in `block in background'  Tasks: TOP => precomputation:fetch_all  (See full trace by running task with --trace)  

Any ideas on what I'm doing wrong? The background thread library was cloned and just modified. Feel free to replace it if you think that's the problem. Any suggestions, any ideas are more than welcome.

rails-api:render json multiple values

Posted: 03 Apr 2016 12:22 AM PDT

I have a model

class Banner < ActiveRecord::Base    validates :title, presence: true, length: { maximum: 50 }    validates :description, length: { maximum: 200 }      belongs_to :document      def img_url      document.medium_url    end  end  

and serializer

class BannerSerializer < ActiveModel::Serializer    attributes :id, :title, :description, :img_url, :document_id  end  

When I'm using render json: Banner.all, it response correctly (has "img_url" in the responding

{  "banners": [      {        "id": 1,        "title": "This is title of banner",        "description": "This is long description...",        "img_url": "http://localhost:3000//system/documents/attachments/000/000/023/medium/one-piece.jpg?1459601536",        "document_id": 23      }    ]  }  

But when I want to return with other object by using. example:

render json: {        banners: Banner.all,        blogs: Blog.all,        partners: Partner.all      }  

The responding don't exist "img_url" (it don't use Serializer).

Please help.

How to fix 'Bundler could not install a gem because it needs to create a directory, but a file exists'

Posted: 02 Apr 2016 11:56 PM PDT

bundle update is failing with the following message Bundler could not install a gem because it needs to create a directory, but a file exists

This occurs after editing a line in the Gemfile so that a gem is fetched from Github.

I have never seen this error before, and am struggling to understand how to resolve it or to find any useful information online.

I've ensured that bundler, rubygems and other relevant parts of my system are up-to-date.

The bundler source indicates that this error is raised if coping to the destination fails. Line 117 https://github.com/bundler/bundler/blob/master/lib/bundler/source/git/git_proxy.rb

So I have tried removing the offending gem from the Gemfile, running bundle update and cleaning the gemset to remove all previous traces before re-adding and re-installing the gem.

The error is still raised when fetching from github, but disappears when using rubygems.

How to resolve this?

Where can I find examples of ROR code or ask for code it? [on hold]

Posted: 02 Apr 2016 11:32 PM PDT

I need examples of ROR code.
Is it okay to ask here to code this for me?

For example: At main page we have a text form and when i push "submit" it renders page with the text i submited.
(This is just an example don't code it)

Or maybe here some banks with examples already exist?

Rails 4 Pundit - policies in parent models

Posted: 03 Apr 2016 12:34 AM PDT

I'm trying to use rails 4 with pundit policies.

I have a profile model and a projects model. Projects are HABTM with profiles.

I have a project policy, that has a create? action (set to true).

In my profile show page, I want to allow users to create new projects, if they project policy create action allows it.

<% if policy(@project).create? %>    <%= link_to 'CREATE A PROJECT', new_project_path, :class=>"btn btn-info"  %>  <% end %>  

When I try this, i get a nil policy error. Is it because you can't use project actions inside profile views? If so, how do I fix it so that I can display a new project button on my profile show page?

Rails display related posts by tags with images from carrierwave

Posted: 02 Apr 2016 09:13 PM PDT

I'm trying to retrieve related posts by tags by showing the attached image per related post, uploaded by using the carrierwave gem

Tags were written from scratch but similar to acts-as-taggable-on gem. Below solution picked from Rails count article per tag and related article by tag displays the related posts by title.

<% @posts.each do |post| %>    <% post.tags.each do |tag| %>    RELATED POSTS:  <% tag.posts.each do |related_post| %>  <%= link_to related_post.title + " , ", post %>  <% end %>  <% end %>  <% end %>  

Now I want related posts to be displayed by image instead of title per above. Code displaying image for post is

<%= image_tag(post.cover.url(:thumb)) if song.cover? %>  

How can I fit this in the RELATED POSTS above? I tried

<%= image_tag(related_post.cover.url(:thumb)) if song.cover? %>  

but this throws:

NameError: undefined local variable or method 'related_post' for #<#:0x007fb67db38fb8>

Obviously related_post is not recorded in the carrierwave uploader model since it is different from the tags model. Any help please?

Errors in Angular web app and now showing any views

Posted: 02 Apr 2016 09:36 PM PDT

I'm following a tutorial which is making Angular on Rails. here is a JSFiddle URL for the project: https://jsfiddle.net/dcbavw4e/4/

I'm currently getting 2 errors right now:

 1. Uncaught SyntaxError: Unexpected token .  Error occuring at :     .controller('MainCtrl', ['$scope', 'posts', function($scope, posts) {    2. angular.js:68 Uncaught Error: [$injector:modulerr] Failed to instantiate module flapperNews due to:  Error: [$injector:nomod] Module 'flapperNews' is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that you specify the dependencies as the second argument.  http://errors.angularjs.org/1.4.9/$injector/nomod?p0=flapperNews      at https://ajax.googleapis.com/ajax/libs/angularjs/1.4.9/angular.js:68:12      at https://ajax.googleapis.com/ajax/libs/angularjs/1.4.9/angular.js:2006:17      at ensure (https://ajax.googleapis.com/ajax/libs/angularjs/1.4.9/angular.js:1930:38)      at module (https://ajax.googleapis.com/ajax/libs/angularjs/1.4.9/angular.js:2004:14)      at https://ajax.googleapis.com/ajax/libs/angularjs/1.4.9/angular.js:4447:22      at forEach (https://ajax.googleapis.com/ajax/libs/angularjs/1.4.9/angular.js:341:20)      at loadModules (https://ajax.googleapis.com/ajax/libs/angularjs/1.4.9/angular.js:4431:5)      at createInjector (https://ajax.googleapis.com/ajax/libs/angularjs/1.4.9/angular.js:4356:11)      at doBootstrap (https://ajax.googleapis.com/ajax/libs/angularjs/1.4.9/angular.js:1677:20)      at bootstrap (https://ajax.googleapis.com/ajax/libs/angularjs/1.4.9/angular.js:1698:12)  http://errors.angularjs.org/1.4.9/$injector/modulerr?p0=flapperNews&p1=Erro….googleapis.com%2Fajax%2Flibs%2Fangularjs%2F1.4.9%2Fangular.js%3A1698%3A12)  

I'm not really sure what I'm doing wrong since I did check App name in index.html and app.js but they all match.

Serve JSON from index action to index.html in public folder

Posted: 02 Apr 2016 09:49 PM PDT

I'm trying to create a single page application using Rails as the backend as this is the only backend framework I know. I put all my files in the public folder and also installed npm and jspm so I can use javascript functionality.

What I'm trying to do is have this index action in my movies controller

  def index      @movies = Movie.all      render :json => @movies    end  

send the collection of movies as JSON data to index.html file located in the public folder. I'm using the 'fetch' function to retrieve the data in my client.js file:

let api_url = 'http://127.0.0.1:3000'    fetch(api_url).then(function(resp) {    resp.json().then(function(movies) {      console.log(movies)    })  })  

This results in the following error:

Uncaught (in promise) SyntaxError: Unexpected token <  

I'm not sure what this means and if I'm even going about this correctly. Any help would be greatly appreciated.

No comments:

Post a Comment