Monday, June 20, 2016

trying to webscrape a website but getting 403 forbidden | Fixed issues

trying to webscrape a website but getting 403 forbidden | Fixed issues


trying to webscrape a website but getting 403 forbidden

Posted: 20 Jun 2016 07:56 AM PDT

I'm trying to access ticketmasters website to get prices of events.

However i'm running into issues where httpparty returns a 403 error on trying to scrape the website

This is all i have

page = HTTParty.get(ticketmaster_url)   doc = Nokogiri::HTML(page)  

And the 403 appears on the get call

Thanks Sam

Rails webmock stubbing out localhost api call

Posted: 20 Jun 2016 07:50 AM PDT

I have a controller, which has a post_review action which calls a Rest Client API call.

def post_review    ...    headers = { "CONTENT_TYPE" => "application/json",                "X_AUTH_SIG" => Rails.application.secrets[:platform_receiver_url][:token] }    rest_client.execute(:method => :put,                        :url => Rails.application.secrets[:platform_receiver_url][:base_url] + response_body["application_id"].to_s,                        :content_type => :json,                        :payload => response_body.to_json,                        :headers => headers)    document_manual_result(response_body)    delete_relavent_review_queue(params[:review_queue_id])    ...  end  

The document_manual_result is a logging method and the delete_relavent_review_queue is a callback type method which will delete the item.

I have written several tests that are testing the side effects of the post_review action, namely that it documents that I have sent off the result(aka:response_body) and that I delete another object.

  describe "Approved#When manual decision is made" do      it "should delete the review queue object" do        e = Event.create!(application_id: @review_queue_application.id)        login_user @account        post :post_review, @params        expect{ReviewQueueApplication.find(@review_queue_application.id)}.to raise_exception(ActiveRecord::RecordNotFound)      end        it "should update the event created for the application" do        e = Event.create!(application_id: @review_queue_application.id)        login_user @account        post :post_review, @params        expect(Event.find(e.id).manual_result).to eq(@manual_result)      end    end  

Before I turned on RestClient the tests worked, but now that rest client is executing It is breaking the specs. I would like to stub just the rest_client.execute portion of the controller action, so I can test the other side effects of the method. The URL I have it pointing to is localhost:3001 so I tried:

 stub_request(:any, "localhost:3001")   

I used it within, my before block which didn't do anything and I tried it inside the actual test it block just before I post :post_review, @params and Webmock seems to be doing nothing. What I thought webmock does, is that it is listening to any requests being made to a specific URL and it returns a success by default or a block of options that you specify. I am not sure I am using this correctly.

How to handle exception in grape (rails), if database connection is null or access denied

Posted: 20 Jun 2016 07:46 AM PDT

I want to handle exception while writing API in rails using Grape gem. if database connection has been null or username or password are wrong then it has through error. but i want to handle it using exception.

Update not redirecting to show - missing template

Posted: 20 Jun 2016 07:43 AM PDT

I've taken a look at the other questions about missing templates, but their mistakes don't seem to be the case here. e.g. I don't have remote: true in my form.

I have a form to update an Inspection:

<%= simple_form_for @inspection , :html => {  multipart: true } do |f| %>    <%= f.error_notification %>      <%= hidden_field_tag :building_id, @building.id %>      <div class="form-inputs">        <div class="form-group select optional inspection_building">          <label class="select optional control-label" for="inspection_building_id">Building: </label>           <%= @building.name %>        </div>         <div class="form-group select optional inspection_building">          <label class="select optional control-label" for="inspection_building_id">Bot: </label>          <%= Inspection::BOTS[0] %>       </div>       <%= f.input :inspection_label %>       <%= f.input :notes %>        <div class="controls form-group integer required">          <div>              <%= f.input :capture_date, as: :date, html5: true, label: "Capture Date of Inspection Images" %>          </div>      </div>            <% if true #if @inspection.new_record? #only show for new inspections %>          <%= render 'attach_image_meta_files' %>            <br>            <div class="controls form-group integer required">              <label class="integer required control-label" for="layout_type">What Excel file Layout, Filip?</label>              <div>              <%= select_tag :layout_type,                 options_for_select(%w[New Old])              %>                </div>          </div>            <div class="controls form-group integer required">              <label class="integer required control-label" for="image_file_prefex">Image File Prefex? (this combined the the Excel file Image Number needs to match the image file names uploaded)</label>              <div>                  <%= text_field_tag :image_file_prefex, 'DSC_', class: 'required form-control' %>              </div>          </div>              <div class="controls form-group integer required">              <label class="integer required control-label" for="first_row_of_data">First Row of Data?</label>              <div>                  <%= number_field_tag :first_row_of_data, 3, in: 1..10, class: 'numeric integer required form-control' %>              </div>          </div>            <div class="controls form-group integer required">              <label class="integer required control-label" for="top_floor">What is the Top Floor of the Building (what floor number do you want the Roof to be)?</label>              <div>                  <%= number_field_tag :top_floor, 1, in: 1..100, class: 'numeric integer required form-control' %>              </div>          </div>        <% end %>   </div>  <br>  <span id="loading-msg" class="text-center display_loading"  style="position: absolute; top: 200px; left: 0; bottom: 0; right: 0; margin: auto; color:blue; display:none">              <%= image_tag "ajax-loader.gif",  alt: "loading", id: "loading_image", class: "center-block" %>Uploading Images and Processing.  This will take a while!  </span>  <script>  $(".display_loading").on("click", function() {             $('#loading-msg').show();      })  </script>      <div class="form-actions">          <% if @inspection.new_record? #only show for new inspections %>          <%= f.button :button,  class: 'btn btn-success display_loading' do %>            <span class="glyphicon glyphicon-arrow-up" aria-hidden="true"></span> Upload and Process Inspection Files          <% end %>      <% else %>           <%= f.button :submit,  class: 'btn btn-success' %>      <% end %>    </div>  <% end %>  

Here's the controller:

def update      respond_to do |format|        if @inspection.update(inspection_params)                   Thread.new do                        process_image_and_excel_metadata_files                    end                format.html { redirect_to @inspection,                        notice: 'Inspection was successfully Updated. Images are being Processed in the background. ~1 minute per 50 files' } and return            else          format.html { render :edit }          end      end    end  

So it should redirect to the show view (which exists, of course).

Instead, it tries to find inspections/update and fails:

Missing template inspections/update, application/update with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :coffee, :jbuilder]}.

incorrectly requiring a module in rails

Posted: 20 Jun 2016 07:47 AM PDT

I want to set up a module in my rails application under /lib but I'm getting import errors about methods not existing. This is what I want

/lib/validator.rb

module Validation      def get_validator(type)      return case type        when "password"          PasswordValidator.new        else          "none"        end    end      class PasswordValidator      def validate(password)        return 'strong'      end    end    end  

and then in my controller I want to import this module (not file) and use it like this:

require 'validator'    class AuthenticationController < ApplicationController    def validate_password      password = params[:password]      result = Validation.get_validator('password').validate() **error here**      render :json => {'strength': result}    end  end  

first thins is, I'm getting an error

undefined method 'get_validator' for validator module

so I guess I'm doing the require wrong, or calling it wrong

a couple questions:

what's usually the practice in ruby/rails, requiring a file or a module? how can I fix my import and usage of the imported module

Sending an AJAX request to rails controller, then a HTTParty to external service, which returns JSON to output in .html.erb file

Posted: 20 Jun 2016 07:52 AM PDT

I am attempting to send an AJAX response to my rails controller, which will then send a HTTParty request to an external service. I am doing it this was because their would be a CORS issue if I sent a AJAX request straight from the JS. After the HTTParty, I need to receive the JSON it returns and output that on the .html.erb file. It seems complicated, but I just need a little bit of assistance implementing its functionality;

We begin by sending the AJAX request to our rails backend by doing the following.

$.ajax({          url: "/gametitle",          type: "post",          data : {title: gametitle}      });  

The data send would look something like {"title": "AC3"}

This AJAX request would go to the routes file which looks like this:

   match '/gametitle' => 'loadtests#gametitle_post', via: :post  

This route would then send it to the loadtest controllers gametitle_post method. Which looks like the following:

 def gametitle_post      @gametitle = params[:title]      HTTParty.post("http://supersecret.com/loadtests",      :query => { :title => @gametitle })          render nothing: true   end  

This is meant to receive the AJAX request, store the param sent, and then send a HTTParty request to receive JSON. But this is where I am stuck at the moment. Whenever I try to output the variable in @gametitle by console.log('<%= @gametitle %> it is an empty string.

Thank you for any help that you can give me in assisting me with this problem.

Ruby & Rails - Seeding Data With Associations from a CSV

Posted: 20 Jun 2016 07:52 AM PDT

I'm new to Ruby & Rails and I am trying to seed a rails database with data from two CSV files and create an association between them. Here is what I have so far:

seeds.rb

require "csv"    City.destroy_all  Skyscraper.destroy_all    csv_text = File.read(Rails.root.join("public", "cities.csv"))  csv = CSV.parse(csv_text, :headers => true)  csv.each do |row|    c = City.new    c.city_name = row["city_name"]    c.country = row["country"]    c.save    puts "#{c.city_name}, #{c.country} saved"  end    csv_text = File.read(Rails.root.join("public", "skyscrapers.csv")).encode!("UTF-8", "binary", invalid: :replace, undef: :replace, replace: "?")  csv = CSV.parse(csv_text, :headers => true, :encoding => "iso-8859-1:utf-8")  csv.each do |row|    s = Skyscraper.new    s.rank = row["rank"]    s.name = row["name"]    s.city_name = row["city_name"]    s.country = row["country"]    s.heightM = row["heightM"]    s.heightF = row["heightF"]    s.floors = row["floors"]    s.completedYr = row["completedYr"]    s.materials = row["materials"]    s.use = row["use"]    city = City.find_by(city_name: row["city_name"])    s.city = city    s.save    puts "#{s.rank}, #{s.name}, #{s.city_name}, #{s.country}, #{s.heightM}, #{s.heightF}, #{s.floors}, #{s.completedYr}, #{s.materials}, #{s.use} saved"  end  

migrations:

class CreateCities < ActiveRecord::Migration    def change      create_table :cities do |t|        t.string :city_name        t.string :country      end    end  end          class CreateSkyscrapers < ActiveRecord::Migration    def change      create_table :skyscrapers do |t|        t.integer :rank        t.string :name        t.string :city_name        t.string :country        t.integer :heightM        t.integer :heightF        t.integer :floors        t.integer :completedYr        t.string :materials        t.string :use        t.references :city, index: true, foreign_key: true      end    end  end  

schema.rb

ActiveRecord::Schema.define(version: 20160603210529) do      # These are extensions that must be enabled in order to support this database    enable_extension "plpgsql"      create_table "cities", force: :cascade do |t|      t.string "city_name"      t.string "country"    end      create_table "skyscrapers", force: :cascade do |t|      t.integer "rank"      t.string  "name"      t.string  "city_name"      t.string  "country"      t.integer "heightM"      t.integer "heightF"      t.integer "floors"      t.integer "completedYr"      t.string  "materials"      t.string  "use"      t.integer "city_id"    end      add_index "skyscrapers", ["city_id"], name: "index_skyscrapers_on_city_id", using: :btree      add_foreign_key "skyscrapers", "cities"  end  

I am able to complete all migrations and seed the "cities" part of the seed file. But when I seed "skyscrapers", I get the following error:

NoMethodError: undefined method `city=' for #<Skyscraper:0x007fa6ec913be8>  /Users/AndrewSM/.rvm/gems/ruby-2.2.3/gems/activemodel-4.2.6/lib/active_model/attribute_methods.rb:433:in `method_missing'  /Users/AndrewSM/wdi/Projects/skyscrapers/db/seeds.rb:42:in `block in <top (required)>'  /Users/AndrewSM/wdi/Projects/skyscrapers/db/seeds.rb:29:in `<top (required)>'  /Users/AndrewSM/.rvm/gems/ruby-2.2.3/gems/activesupport-4.2.6/lib/active_support/dependencies.rb:268:in `load'  /Users/AndrewSM/.rvm/gems/ruby-2.2.3/gems/activesupport-4.2.6/lib/active_support/dependencies.rb:268:in `block in load'  /Users/AndrewSM/.rvm/gems/ruby-2.2.3/gems/activesupport-4.2.6/lib/active_support/dependencies.rb:240:in `load_dependency'  /Users/AndrewSM/.rvm/gems/ruby-2.2.3/gems/activesupport-4.2.6/lib/active_support/dependencies.rb:268:in `load'  /Users/AndrewSM/.rvm/gems/ruby-2.2.3@global/gems/railties-4.2.6/lib/rails/engine.rb:547:in `load_seed'  /Users/AndrewSM/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.6/lib/active_record/tasks/database_tasks.rb:250:in `load_seed'  /Users/AndrewSM/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.6/lib/active_record/railties/databases.rake:183:in `block (2 levels) in <top (required)>'  Tasks: TOP => db:seed  (See full trace by running task with --trace)  

In seeds.rb, Line 42 refers to "s.city = city" and line 29 refers to "csv.each do |row|". Originally I thought there might have been a conflict between city from the foreign key and a row in the tables also named "city", so I changed to the row heading in each table to "city_name" (these used to be just "city"). However, I get the same error. When I comment out line 42 (s.city = city), everything migrates and seeds, but there is no association.

Thank you in advance for your feedback.

Appium Android CircleCI - testing web app using android emulator and rspec

Posted: 20 Jun 2016 07:29 AM PDT

I am trying to run an rspec test suite through my android emulator on CircleCI. I'm getting errors because I don't think my emulator is communicating with my tests properly which causes an error relating to not recognizing the chrome session. I tried using gradle to build the android environment but I'm unsuccessful. Here is my Circle.yml file. Keep in mind, I need to test web app using rspec tests. So I do not have an apk or unit tests to work from.

machine:    node:      version: 0.12    ruby:      version: 2.3.0    pre:      - wget http://chromedriver.storage.googleapis.com/2.21/chromedriver_linux64.zip      - unzip chromedriver_linux64.zip      - sudo mv -f chromedriver /usr/local/bin    dependencies:    pre:      - gem install bundler      - npm install appium    test:    override:      #bootup emulator      - emulator -avd circleci-android22 -no-audio -no-window:          background: true          parallel: true      # wait for it to have booted      - circle-android wait-for-boot      - sleep 5      #unlock the device      - adb shell input keyevent 82      - sleep 5      - adb shell input touchscreen swipe 370 735 371 735      - sleep 5      #start appium server      - appium:          background: true      - ./gradlew init      - ./gradlew dependencies      - ./gradlew assemble      - ./gradlew check      - ./gradlew connectedAndroidTest      #run tests against emulator      - CI_NAME=circle_ci RAILS_ENV=test bundle exec rake mobile nodes=4 --trace:          parallel: true  

Migrating from Trac to Redmine [Trac2Redmine]

Posted: 20 Jun 2016 07:48 AM PDT

Lately i am trying to move my trac.db to redmine, and i have faced so many bugs etc.

I want to ask is there any stable version of Redmine for migrating, or any good patched migrate_from_trac.rake file?

Currently i have: Redmine 3.2.3 Ruby 2.2.0

My Gemfile:

gem 'sqlite3'  gem "rails", "4.2.5.2"  gem "jquery-rails", "~> 3.1.4"  gem "coderay", "~> 1.1.0"  gem "builder", ">= 3.0.4"  gem "request_store", "1.0.5"  gem "mime-types", (RUBY_VERSION >= "2.0" ? "~> 3.0" : "~> 2.99")  gem "protected_attributes"  gem "actionpack-action_caching"  gem "actionpack-xml_parser"  gem "roadie-rails"  gem "mimemagic"  gem 'arel', '6.0.0.beta2'  gem "activerecord-deprecated_finders", require: "active_record/deprecated_finders"  

Is there anything else you need i will happily answer them too.

My final error is:

ActiveRecord::StatementInvalid: SQLite3::SQLException: near "AND": syntax error: SELECT  "session_attribute".* FROM "session_attribute" WHERE "session_attribute"."sid" =  AND "session_attribute"."name" =  LIMIT 1  

Please answer or ask something, I will return you in 15 hours in worst case.

(This is my first question, so i am open for any comments, sorry for my bad english)

change.log: migrate_from_trac.rake

Fixed: set_inheritance_column & set_primary_key errors.    Fixed: add 'gem' sqlite3 to your Gemfile error.    Fixed: incorrect date time value between trac and redmine.  

I would love to work on a project for this issue. #redmine #trac

Ruby on rails application shows files index instead of the app

Posted: 20 Jun 2016 07:51 AM PDT

I would like to deploy a Ruby on Rails application on the web, and for that matter I have used/deployed the following tools:

  1. Capistrano, to deploy the app files to the target location
  2. The Phusion Passenger module for Apache

I have enabled the Passenger module through sudo a2enmod passenger. I have also installed the passenger gem through the gem installer, and added it to the Gemfile of my rails app. I have then created an Apache virtual host for the app 'myapp'.

myapp.conf

  Alias /myapp /var/www/myapp/current/public    <VirtualHost *:80>      DocumentRoot /var/www/myapp/current/public      SetEnv SECRET_KEY_BASE 592da***************************************     <Directory /var/www/myapp/current/public>       PassengerEnabled on       PassengerResolveSymlinksInDocumentRoot on       PassengerAppRoot /var/www/myapp/current       PassengerAppType rack       PassengerStartupFile config.ru       Allow from all       Options -MultiViews       Require all granted     </Directory>       </VirtualHost>  

The virtual host work, but when I access the URL in a web browser, it shows the files index in /var/www/myapp/current/public instead of the actual app. The reason for this seems to be that passenger is not started, but I can't figure out why. I have tried to tweak the myapp.conf file to help apache and passenger detect the app, but without success.

Could anyone help me fix this? Thanks in advance.

Additional info: the app is deployed on a Raspberry Pi 3 with Raspbian Jessie as OS.

Which to learn first to earn money: Ruby on rails or Python Django? [on hold]

Posted: 20 Jun 2016 07:13 AM PDT

My current goal is to earn money through Upwork . so which one of them has more chances to work as freelancer . what I needed to note that it's not just web development that I want to work in .

What does the method `method` do in `&Unit.method(:new)`?

Posted: 20 Jun 2016 07:35 AM PDT

I'm new to the Rails and trying to figure out the code that im given. What does the method method do in &Unit.method(:new) ? And what is the meaning of &? There's not method method in Unit model and wondering why it can be there. And lastly, I guess the symbol :new creates a new object of Unit ?

class Unit    include ActiveModel::Model      attr_accessor :number   end      class Product    include ActiveModel::Model    .........    .........    def units=(values)      @units = values.map(&Unit.method(:new))    end  end  

Rails wicked gem - bootstrap modal on finish wizard

Posted: 20 Jun 2016 06:58 AM PDT

I have searched over the stackoverflow and internet but could not find a solution.

I have a car modal and I used wicked gem for user to enter data. But on the finish wizard, I would like to show bootstrap modal to user that kind of says, "we have received your car listing request. We will review and get back to you".

Is it technically possible?. The problem is, when a user wants to edit his/her car listing I use the same wicked form.

So if it is possible, how should I understand it is first listing request and he/she is not editing the car modal.

Storing multple arrays in a database rails

Posted: 20 Jun 2016 06:56 AM PDT

I'm pulling data from the UCAS website and storing it in a series of arrays as can be seen below. I want to store these arrays in a database but I'm not sure how to go about doing this. Can anyone help? Ideally in sqlite3 although I plan on changing this to postgres at a later date.

class PagesController < ApplicationController      require 'mechanize'    class Scraper            def process_unis          @mechanize = Mechanize.new            @unis_array = []          page_num = 1          next_page_link = true          # Load initial search page          @page = @mechanize.get('http://search.ucas.com/search/providers?CountryCode=3&RegionCode=&Lat=&Lng=&Feather=&Vac=2&Query=&ProviderQuery=&AcpId=&Location=scotland&IsFeatherProcessed=True&SubjectCode=&AvailableIn=2016')          while  next_page_link              puts "- Scraping uni page: #{page_num}"               # Loops through all unis on page              @page.search('li.result').each do |uni|                  # Sets hash to store all uni data                  uni_info = {}                  uni_name = uni.search('h3').text                  uni_info[:name] = uni_name                  uni_more_courses_link = uni.search('.morecourseslink a').first                  if uni_more_courses_link                      # If there is a link to a courses page for the uni go to that                      uni_info[:courses] = process_courses_page(@mechanize.get(uni_more_courses_link["href"]))                   else                      # Otherwise process all the courses on the current page for that uni                      uni_info[:courses] = process_inline_courses(uni)                  end                  # Add complete uni info to array. This may also be a good place to inset the uni in the database                  @unis_array.push(uni_info)              end              # Checks if there is a next page and navigates if so              next_page_link = @page.search('.pager a[text()=">"]').first              if(next_page_link)                   @page = @mechanize.get(next_page_link["href"])                  page_num += 1              end          end      end        # Just a function to print what has been added. Can be removed if unneeded      def print_course_info(course_info)          puts "--- Adding course: #{course_info[:name]}"           puts "---- Duration: #{course_info[:duration]}"          puts "---- Qualification: #{course_info[:qualification]}"        end            def print_details_info(details_info)            puts "---- link: #{details_info[:url]}"        end          def print_entry_info(entry_info)            puts "---- Requirements: #{entry_info[:req]}"          puts ""      end           # Removes excess spaces and new line characters from duration and qual text      def clean_text(text)          return text.gsub!('  ', '').gsub!("\n", '').gsub!("\r", '')      end        def clean_reqs(text)          return text.gsub!('  ', '').gsub!("\n", '').gsub!("\r", '')      end            def process_inline_courses(uni)          uni_name = uni.search('h3').text          puts "-- Scraping #{uni_name} courses"          courses_array = []            # Loops through all courses, saves their info into a hash and pushes it to an array          uni.search('.courseresult').each do |course|              course_info = {}              course_info[:name]          = course.search('.title').text              course_info[:duration]      = course.search('.durationValue').text              course_info[:qualification] = course.search('.outcomequalValue').text                courses_array.push(course_info)              print_course_info(course_info)          end            puts "--- Scraped #{courses_array.length} courses"          return courses_array      end                def process_courses_page(course_page)          courses_array = []          page_num = 1          next_page_link = true             details_link = true           uni_name = course_page.search('.providerinfo h3').text          # Loops through all course pages for uni          while next_page_link                  puts "-- Scraping #{uni_name} courses page: #{page_num}"              # Loops through all courses and adds their info into a hash              course_page.search('ol.resultscontainer li').each do |course|                  course_info = {}                    # Removes excess html which was interferring with text                  course.search(                      '.courseinfoduration span,                        .courseinfoduration br,                        .courseinfooutcome span,                        .courseinfooutcome br').remove                  # Sets all data in hash                  course_info[:name]          = course.search('.courseTitle').text                  course_info[:duration]      = clean_text(course.search('.courseinfoduration').text)                  course_info[:qualification] = clean_text(course.search('.courseinfooutcome').text)                  # Pushes course hash to course array                  courses_array.push(course_info)                  print_course_info(course_info)                      details_link = course.search('div.coursenamearea a').first                      if details_link                       # If there is a link to a courses page for the uni go to that                         course_info[:detail] = process_course_details(@mechanize.get(details_link["href"]))                         end               end                       # Checks if there is a next page and navigates to it if so            next_page_link = course_page.search('.pager a[text()=">"]').first                      if(next_page_link)                   course_page = @mechanize.get(next_page_link["href"])                  page_num += 1              end          end  end          def process_course_details(course_details)          details_array =[]          details_link = true           entry_link = true                    details_info = {}                  # Sets all data in hash                  details_info[:url]          = course_details.search('div.coursedetails_programmeurl a')                    details_array.push(details_info)                  print_details_info(details_info)                   entry_link = course_details.search('ul.details_tabs a').first               if entry_link                   details_info[:entry] = process_entry(@mechanize.get(entry_link["href"]))               end          end          def process_entry(entry_req)          entry_array = []          entry_link = true                     entry_info = {}                  # Sets all data in hash                  entry_info[:req]          = entry_req.search('li.qual-element.qual_range').text.strip                    entry_array.push(entry_info)                  print_entry_info(entry_info)      end        scraper = Scraper.new  scraper.process_unis    end      end   

Connection refused for mail catcher 1025 port in rails application run using docker compose

Posted: 20 Jun 2016 06:49 AM PDT

Im using mailcatcher in my Rails application to catch emails in the development environment. Im using Docker Compose to run my rails app in dev. Below is my docker-compose.yml

version: '2'  services:    db:      image: postgres    mailcatcher:      image: yappabe/mailcatcher      ports:        - "1025:1025"        - "1080:1080"    web:      build: .      command: bundle exec rails s -p 3000 -b '0.0.0.0'      volumes:        - .:/myapp      ports:        - "3000:3000"      links:        - db        - mailcatcher  

In my config/environments/development.rb, i have the following:

config.action_mailer.raise_delivery_errors = true  config.action_mailer.perform_deliveries = true  config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }  config.action_mailer.delivery_method = :smtp  config.action_mailer.smtp_settings = { address: 'mailcatcher', port: 1025 }  

Now when my application tries to send email i get the following error:

Connection refused - connect(2) for "localhost" port 1025  

As you can see in my development.rb,i have set the address in smtp_settings to 'mailcatcher' and not 'localhost'. Why am i still getting the connection refused for localhost port 1025 error ?

Please Help. Thank You.

Rails 4 - Select using eager_load

Posted: 20 Jun 2016 07:54 AM PDT

I'm trying to select only certain columns using eager_load, but I'm facing the problem that it cancels my 'select'.

Model:

class Timeline < ActiveRecord::Base      belongs_to :timeline_category, foreign_key: :timeline_category_id      belongs_to :user, foreign_key: :user_id      scope :with_relations, -> { eager_load(:timeline_category).eager_load(:user).order(created_at: :desc)   end  

Query:

Timeline.select('timelines.*, users.username, timeline_categories.icon').eager_load(:timeline_category).eager_load(:user)  

I tried also:

Timeline.select('timelines.*, users.username, timeline_categories.icon').with_relations  

For some reason, it keeps selecting all columns of all 3 tables. How can I fix it?

how to change string _id to mongoid to delete record from mongo database

Posted: 20 Jun 2016 06:40 AM PDT

this doesn't delete the record from the database

collection.delete_one({"_id": ObjectId("574acf1e7af7b6073c2cf9ae")});

Setting up a relation to a partialy embedded document in Mongoid

Posted: 20 Jun 2016 06:31 AM PDT

I have a class (let's call it cake) that has 2 user relations on them, one is byUser, the other one is toUser. In the database, it's stored in the collection Cakes like this:

# just to get the idea, not literaly  {      byUser: {          __type:    'Pointer',          className: '_User',          objectId:  's19jYhbNwx'      },      toUser: {          __type:    'Pointer',          className: '_User',          objectId:  '9Kv6CxXuSO'      },      type: 'birthday'  }  

I have a user class, that has the attribute objectId that I need to set as a primary key.

My question is basically, how can I setup a relation in Mongoid on the User and the Cake model, since the objectId in the Cake model is embedded in the byUser and toUser BSONs.

I need this for eager loading of the User model, since that will impact my performance greatly.

One more important thing, I can't change the database structure.

ruby on rails: ancestry not working

Posted: 20 Jun 2016 06:26 AM PDT

i'm using ancestry gem to build a hierarchy tree. I installed the gem, and I can select a node has a parent, but when I do for example this <% @posts.root.children.each do |page| %> it gives me

`undefined method `root' for #<Post::ActiveRecord_Relation:0x7094790>`  

What am I doing wrong?

In the post model I have "has_ancestry". And I have the column ancestry in the database too

creating app on digital ocean issue

Posted: 20 Jun 2016 07:22 AM PDT

I've got a guide which im following here.

Now this is working perfectly so far, however, I'm up to the part where I need to do the DB migrate and I've ran into this error:

Psych::SyntaxError: (<unknown>): could not find expected ':' while scanning a simple key at line 6 column 1  

Now I'm thinking it may be because when I did a rake secret it only returned a long number, rather than a key and a token.

Any thoughts/opinions on this?

Multiple forms with same radio buttons and labels don't select properly (Ruby on Rails)

Posted: 20 Jun 2016 06:07 AM PDT

(Ruby on Rails application)

A quick explanation of how the this part of my application works: Users can join a Conversation, and can send a Message inside that Conversation. On the conversation show page, all of the different users' messages are displayed. When a user sees another user's message, he can click a "like" button for that message. Clicking that button creates a new instance of a Reaction model. The new instance of Reaction has a belongs_to association to the "like" ReactionExample object, which I uploaded. ReactionExamples are different little possible reactions that I myself upload, for example "like", "love", "hate", etc... Think of them like emojis, and on each message users can create a Reaction object which refers to the original ReactionExample object which I've uploaded (this way user's have examples to choose from and don't create an entirely new one each time, they just refer back to the example through a belongs_to association).

I would paste in the code for all of the above models, but as you will soon see it's irrelevant to the problem I'm having. My problem is purely related to my forms, more specifically my radio buttons and labels. The only associations you need to know are that a Conversation has many Messages, a User has many Messages, a Message has many Reactions, and each Reaction belongs to a ReactionExample. (therefore each reaction refers to a reactionExample through the reaction's :reaction_example_id)

On each conversations/show page, each user's message for that particular conversation is on display. Users in the conversation can interact with all of the other messages in the conversation, including reacting to the messages. This is where my forms come in.

Here are the contents of my conversations#show action:

@conversation = Conversation.find(params[:id])  @messages = @conversation.messages.all  @reaction_examples = ReactionExample.all #These are "like", "love", "hate", etc..  

And here is the content of my conversations/show view. I have a block which iterates through each message, and each message renders a new form for itself, which therefore gives a user the option to create a new reaction for all of those iterated messages.

<% @messages.each do |message| %>    <p><%= message.user.first_name %> says <%= message.content %></p>    <%= form_for message.replies.build, url: reaction_conversation_path(@conversation) do |f| %>      <%= f.hidden_field :message_id, value: message.id %>      <%= f.hidden_field :user_id, value: current_user.id %>      <% @reaction_examples.each do |example| %>        <%= f.radio_button :reaction_example_id, example.id %>        <%= f.label :reaction_example_id, example.phrase, value: example.id %>      <% end %>      <%= f.submit "Send reaction" %>    <% end %>  <% end %>  

HERE IS WHERE THE PROBLEM PRESENTS ITSELF: as you can see, on a single page (conversations/show) there will be multiple forms to send a reaction. Each of these forms will have one or more radio buttons and labels with the same value, just a different :message_id on each form. This is a problem, because I would like to be able to select a reaction radio button by clicking its label, but there are multiple forms on this page with radio buttons WITH THE SAME LABEL VALUE. So what's happening is, when I click a radio button label on the third or fourth message down on the page, it selects the associated radio button on the first message up on the page. It's because they technically have the same value since the same ReactionExample id's are being used in each radio button in the forms. So this weird thing happens where I select a label on a form and it won't affect that form itself, it just affects the first displayed form on the page.

Has anyone come across a situation like this and though of a solution? I'm trying to figure out a way around it but I'm getting nowhere. Sorry if my question was too thorough. Any help or tips are really really appreciated. Thank you all so much in advance.

Rails Capistrano precompile assets locally not loading on production

Posted: 20 Jun 2016 06:46 AM PDT

i deployed my rails app to the AWS server and tried running the rake assets:precompile locally and uploading on deploy .due to the out of memory issue on the server ,

this is my deploy.rb

# config valid only for current version of Capistrano  lock '3.5.0'    set :application, 'fullpower_tee'  set :repo_url, 'git_repo' # Edit this to match your repository  set :branch, :master  set :deploy_to, '/home/deploy/fullpower_tee'  set :pty, true  set :linked_files, %w{config/database.yml config/application.yml}  set :linked_dirs, %w{bin log tmp/pids tmp/cache tmp/sockets vendor/bundle public/system public/uploads}  set :keep_releases, 2  set :rvm_type, :user  set :rvm_ruby_version, 'ruby-2.2.1'      set :puma_rackup, -> { File.join(current_path, 'config.ru') }  set :puma_state, "#{shared_path}/tmp/pids/puma.state"  set :puma_pid, "#{shared_path}/tmp/pids/puma.pid"  set :puma_bind, "unix://#{shared_path}/tmp/sockets/puma.sock"    #accept array for multi-bind  set :puma_conf, "#{shared_path}/puma.rb"  set :puma_access_log, "#{shared_path}/log/puma_error.log"  set :puma_error_log, "#{shared_path}/log/puma_access.log"  set :puma_role, :app  set :puma_env, fetch(:rack_env, fetch(:rails_env, 'production'))  set :puma_threads, [0, 8]  set :puma_workers, 0  set :puma_worker_timeout, nil  set :puma_init_active_record, true  set :puma_preload_app, false      namespace :deploy do    namespace :assets do        Rake::Task['deploy:assets:precompile'].clear_actions        desc 'Precompile assets locally and upload to servers'      task :precompile do        on roles(fetch(:assets_roles)) do          run_locally do            with rails_env: fetch(:rails_env) do              execute 'bin/rake assets:precompile'            end          end            within release_path do            with rails_env: fetch(:rails_env) do              old_manifest_path = "#{shared_path}/public/assets/manifest*"              execute :rm, old_manifest_path if test "[ -f #{old_manifest_path} ]"              upload!('./public/assets/', "#{shared_path}/public/", recursive: true)            end          end            run_locally { execute 'rm -rf public/assets' }        end      end      end  end    namespace :deploy do      after :restart, :clear_cache do      on roles(:web), in: :groups, limit: 3, wait: 10 do        # Here we can do anything such as:        # within release_path do        #   execute :rake, 'cache:clear'        # end      end    end    end  

and my config/environments/production.rb

  # Compress JavaScripts and CSS    config.assets.compress = true      # Don't fallback to assets pipeline if a precompiled asset is missed    config.assets.compile = false      # Generate digests for assets URLs    config.assets.digest = true      # config.assets.precompile += %w( *.css *.js )      # Add the fonts path    config.assets.paths << "#{Rails.root}/app/assets/fonts"      # Precompile additional assets    config.assets.precompile += %w( .svg .eot .woff .ttf )    config.assets.precompile += %w( *.js )    config.assets.precompile += [ 'admin.css',                                  'bootstrap.css',                                  'dark-red-theme.css',                                  'style.css',                                  'jquery.growl.css',                                  'default-theme.css',                                  'font-awesome.css',                                  'jquery.simpleLens.css',                                  'jquery.smartmenus.bootstrap.css',                                  'nouislider.css',                                  'sequence-theme.modern-slide-in.css',                                  'slick.css',                                  'admin/app.css',                                  'admin/cart.css',                                  'admin/foundation.css',                                  'admin/normalize.css',                                  'admin/help.css',                                  'admin/ie.css',                                  'autocomplete.css',                                  'application.css',                                  'foundation.css',                                  'home_page.css',                                  'login.css',                                  'markdown.css',                                  'myaccount.css',                                  'normalize.css',                                  'pikachoose_product.css',                                  'product_page.css',                                  'products_page.css',                                  'shopping_cart_page.css',                                  'signup.css',                                  'site/app.css',                                  'sprite.css',                                  'tables.css',                                  'cupertino/jquery-ui-1.8.12.custom.css',# in vendor                                  'scaffold.css' # in vendor                                  ]  

and my application.rb has

config.assets.initialize_on_precompile = false  

the capistrano deploy is successfully able to precompile assets locally and then uploading it to the shared/public directory , but i cannot see the assets loading on my website , been stuck in this issue since a day Please help !

this is my browser log enter image description here

ruby on rails query on jsonb field does not work

Posted: 20 Jun 2016 06:39 AM PDT

I have a table in the PostgreSQL 9.5 with jsonb column called segmented_data and I have a record with some data in this field

ProjectKeyword.first  => #<ProjectKeyword:0x007fa83a17e7f8 id: 2201, project_id: 79, keyword_id: 2201, segmented_data: {"keyword_value"=>"land for sale", "dimension_value"=>{"Property type"=>"Land"}}>  

How I can find this record in the database by value of one of the keyw of segmented_data? I have tried:

ProjectKeyword.where("segmented_data ->> 'keyword_value' = 'land for sale'").first  => nil     ProjectKeyword.where('segmented_data @> ?',  {keyword_value: 'land for sale'}.to_json).first  => nil    ProjectKeyword.where('segmented_data @> ? ', '{"keyword_value":"land for sale"}' ).first  => nil  

What am I doing wrong in these queryes?

EDITED

My Model

class ProjectKeyword < ApplicationRecord    serialize :segmented_data, JSON    belongs_to :project    belongs_to :keyword    has_many :project_keyword_dimensions    has_many :dimensions, through: :project_keyword_dimensions      validates :project_id, :keyword_id, presence: true  end  

Migration

class AddSegemtnedDataToProjectKeywords < ActiveRecord::Migration[5.0]    def change      add_column :project_keywords, :segmented_data, :jsonb, default: '{}'      add_index  :project_keywords, :segmented_data, using: :gin    end  end  

Rails + Google Query Language Reference (Version 0.7), not able to get json format result

Posted: 20 Jun 2016 05:12 AM PDT

I am using Query Language Reference (Version 0.7) https://developers.google.com/chart/interactive/docs/querylanguage for getting response from google spreadsheet in ruby/rails so want json as output on certain query.

Request: http://www.example.com/mydatasource?tq=select Col1

Response: google.visualization.Query.setResponse({version:'0.6',reqId:'0',status:'ok',sig:'6099996038638149313',table:{cols:[{id:'Col1',label:'',type:'number'}],rows:[{c:[{v:1.0,f:'1'}]},{c:[{v:2.0,f:'2'}]},{c:[{v:3.0,f:'3'}]},{c:[{v:1.0,f:'1'}]}]}});

how can I get response as pure json?

e.g.

{version:'0.6',reqId:'0',status:'ok',sig:'6099996038638149313',table:{cols:[{id:'Col1',label:'',type:'number'}],rows:[{c:[{v:1.0,f:'1'}]},{c:[{v:2.0,f:'2'}]},{c:[{v:3.0,f:'3'}]},{c:[{v:1.0,f:'1'}]}]}}

Unable to find any reference, Thanks!

Multiple tables using one model in rails?

Posted: 20 Jun 2016 07:48 AM PDT

Is there a way to use one model to write to different tables if all of the tables have the same fields and attributes? I have first table with 3 millions of records. so I am thinking to make another table like 'table_name_1' to make fast querying in future.

I would like to use one model but be able to decide which table it accesses.

Anyone know if this is possible?

Chrome WebPush notifications payload encryption in ruby on rails

Posted: 20 Jun 2016 04:40 AM PDT

I have implemented webpush notification in ruby on rails using ruby(version = 1.9) and rails(version = 3.2) but it shows nil data in notification because it needs some encryption.

As per the google document, I have to encrypt payload data in given format only and then it will work. So I tried a gem webpush but the gem has only been written to support ruby 2.3. Then I tried to implement elliptic cryptography but again I'm getting nil in the gcm response So I am not sure about my encryption as well.

Thanks in advance!

How to resolve Rails issue- An error occurred while installing mysql2 (0.4.4), and Bundler cannot continue

Posted: 20 Jun 2016 04:41 AM PDT

you may get this issue while working on Rails 4.0 and above version along with Ruby 2.0 and above.

this is very common issue if you are going ahead with up gradation of Rails environment.

Steps to be follow-

For Windows System-

1.Download the the newest mysql-connector to c:\mysql-connector folder   2. gem install mysql2 -- '--with-mysql-include="C:\mysql-connector\include" --with-mysql-lib="C:\mysql-connector\lib"  

Hopefully it will work for you. For Ubuntu System-

1. open terminal (Ctrl+alt+T)  2. $ sudo apt-get install libmysqld-dev  Run bundle from your project directory   3. $ bundle  

This will resolve the issue.We can also see the solution here.

https://randvblog.wordpress.com/2016/06/20/how-to-resolve-rails-issue-an-error-occurred-while-installing-mysql2-0-4-4-and-bundler-cannot-continue-make-sure-that-gem-install-mysql2-v-0-4-4-succeeds-before-bundling/

ActiveRecord in production mode without rails

Posted: 20 Jun 2016 04:58 AM PDT

I want to use activerecord without rails. I know that in rails we can use the production mode as RAILS_ENV=production but how do I use production mode in activerecord without rails?

Creating associated model with rails

Posted: 20 Jun 2016 04:53 AM PDT

I am trying to create associated models with Rails. My models are Financial which have many documents and Document which belongs to Financial. A working code when creating the associated model could be

def create    @financial = Financial.find(2)    @document = @financial.documents.create(document_params)    ...  end  

In my view I have a form which looks like this to select the right Financial

<%= form_for Document.new do |f| %>  <%= f.collection_select :financial_id, Financial.all, :id, :description %>  <%= f.submit %>  

I do see the right parameters being transferred in the log when I submit the form

"financial_id"=>"3"  

So I figured I would just need to change the initial code to:

def create    @financial = Financial.find(params[:financial_id])    @document = @financial.documents.create(document_params)    ...  end  

but I get a "Couldn't find Financial with 'id'=". I have tried other things including:

  @financial = Financial.find_by(id: params[:financial_id])  

Without much success. Could anyone give me the appropriate syntax please? Thanks.

deleteing record from mongodb from application_controller.rb file

Posted: 20 Jun 2016 04:25 AM PDT

db.table.remove({"_id": ObjectId("574acf137af7b6073c2cf9ad")});

It doesn't work in the file.

I need this to be done so that I can replace the id with the paramas['id']

  Rails.logger.info "inside method"    Rails.logger.info params['id']    dbname = Rails.configuration.db.dbname    host = Rails.configuration.db.host    port = Rails.configuration.db.port    table = Rails.configuration.db.table    hostname = "#{host}:#{port.to_s}"    db = Mongo::Client.new([ hostname ], :database => dbname)    collection = db[table]  

No comments:

Post a Comment