Sunday, November 6, 2016

Active admin multiple image upload with paperclip Rails 5 | Fixed issues

Active admin multiple image upload with paperclip Rails 5 | Fixed issues


Active admin multiple image upload with paperclip Rails 5

Posted: 06 Nov 2016 06:50 AM PST

I use Active admin and I need upload photos for my projest. How can I do it? My code:

class Project < ApplicationRecord    has_many :images    accepts_nested_attributes_for :images  end    class Image < ApplicationRecord    belongs_to :project    has_attached_file :image    validates_attachment_presence :image  end      ActiveAdmin.register Project do     permit_params :project_name , :project_location , :project_status , :project_area , :project_prices , :project_info , :project_description , :image , :image_file_name, :image_content_type, :image_file_size, :image_updated_at      index do      column :project_name      column :project_description      actions    end      form :html => { :enctype => "multipart/form-data" } do |f|      f.inputs 'Project Info' do        f.input :project_name        f.input :project_description      end        f.inputs do        f.has_many :images  do |p|          p.input :image , as: :file        end      end        f.actions    end    end  

With this code i can create Project without image. But i can't add any image to db. Waiting for help !!!

translating javascript written code into ruby code concerning spliting excel data into two dimensional variables

Posted: 06 Nov 2016 06:49 AM PST

Below is a javascript written code for splitting copy & pasted excel data. My purpose is to split given excel data into each unit cell and reuse them in the HTML side. My problem that I was able to write it in Javascript code but having trouble rewriting it in Ruby. I need some help here..

var data = $('textarea[name=excel_data]').val();  console.log(data);  var rows = data.split("\n");  var l = rows.length - 3;  console.log(l);  var table = $('<table class="table table-bordered" />');  var datas = new Array();  for(var y in rows) {    var cells = rows[y].split("\t");    var row = $('<tr />');    datas[y] = new Array();    for(var x in cells) {      row.append('<td id='+y+x+'>'+cells[x]+'</td>');      datas[y][x] = cells[x];      $('#excel_table_a'+l+'_'+y+x).html(datas[y][x]);      $('#excel_table_b'+l+'_'+y+x).html(datas[y][x]);      $('#excel_table_c'+l+'_'+y+x).html(datas[y][x]);      $('#excel_table_d'+l+'_'+y+x).html(datas[y][x]);      $('#excel_table_e'+l+'_'+y+x).html(datas[y][x]);      $('#excel_table_z'+l+'_'+y+x).html(datas[y][x]);    } //data generate    table.append(row);  }  

This is the Ruby code I tried my best for it.

data = @bindo.bindo_input  rows = data.split("\n")  length = rows.length - 3  @datas = Array.new  cells = Array.new { }  rows.length.each do |y|    cells << rows[y].split("\t")    @datas[y] = Array.new    cells.length.each do |x|      @datas[y][x] = cells[x]    end  end  

when I try to run this code on Rails, I get an error message like this

undefined method `each' for 7:Fixnum  

I would really appreciate you for any help.

Can I use gitlab-rails to create a new rails app?

Posted: 06 Nov 2016 06:45 AM PST

I have installed gitlab with the omnibus package on a Ubuntu-based machine, on which I'm trying to develop a rails app as well. Since gitlab installs ruby and the rails framework for itself, could I be able to use it to create new rails applications? When I run gitlab-rails new app_name under my home directory I get an error message that reads:

/usr/bin/gitlab-rails error: could not load /opt/gitlab/etc/gitlab-rails/gitlab-rails-rc  Either you are not allowed to read the file, or it does not exist yet.  You can generate it with: sudo gitlab-ctl reconfigure  

And when I run sudo gitlab-rails new app_name I get:

Can't initialize a new Rails application within the director of another, please change to a non-Rails directory first.  Type 'rails' for help  

I've tried to run this under newly created, empty directories, but keep getting the same message. I assume gitlab-rails tries to create the new app under the gitlab project directory for some reason?

Facebook Profile Image & Data using Devise in Rails

Posted: 06 Nov 2016 06:35 AM PST

I'm just starting to learn Rails so please forgive the dumb question. In my web app, I was able to set up devise successfully and then link it Facebook using Omni-auth gem. However, two things occur.

1) A person who signs in using Facebook doesn't register as logged in - the Navbar still shows "Login"

2) I added an avatar so users can show their profile picture. While a regular devise user can added it without a problem. Someone who logs in with Facebook doesn't have their image displayed.

Any help that you guys can give me would be so amazing, I feel like the issue might be in my user.rb file. I have listed below all my relevant code and my github url. Thank you again :)

Github-url: https://github.com/OmarZV/TY2

User.rb

class User < ApplicationRecord      devise :database_authenticatable, :registerable,       :recoverable, :rememberable, :trackable, :validatable, :omniauthable, omniauth_providers: [:facebook]  has_attached_file :avatar, :styles => { :medium => "300x300>", :thumb => "100x100#" }, :default_url => "/images/:style/missing.png"  validates_attachment_content_type :avatar, :content_type => /\Aimage\/.*\Z/  def self.from_omniauth(auth)    where(provider: auth.provider, uid: auth.uid).first_or_create do |user|  user.email = auth.info.email  user.password = Devise.friendly_token[0,20]  user.name = auth.info.name  user.image = auth.info.image    end  end  end  

Application_controller.rb

class ApplicationController < ActionController::Base    protect_from_forgery with: :exception   before_action :configure_permitted_parameters, if: :devise_controller  protected    def configure_permitted_parameters  devise_parameter_sanitizer.permit(:sign_up, keys: [:username, :avatar])  devise_parameter_sanitizer.permit(:account_update, keys: [:username, :avatar])    end  end  

Application.html.erb

<div class="collapse navbar-collapse" id="navbar-collapse-1">      <ul class="nav navbar-nav navbar-right">          <li><%= link_to 'Posts', '#' %></li>                    <% if current_user %>          <li><%= link_to 'Edit Profile',edit_user_registration_path %></li>          <li><%= link_to 'Logout', destroy_user_session_path, method: :delete %></li>            <li class="round-image-50"><%= image_tag(current_user.avatar.url(:thumb)) %></li>            <% else %>          <li><%= link_to 'Login', new_user_session_path %></li>          <% end %>      </ul>  </div>  </div><!-- /.navbar-collapse -->  

Omniauth_Callbacks_Controller

class Users::OmniauthCallbacksController<Devise::OmniauthCallbacksController  def facebook  # You need to implement the method below in your model(e.g.app/models/user.rb)  @user = User.from_omniauth(request.env["omniauth.auth"])  if @user.persisted?  sign_in_and_redirect @user, :event => :authentication #this will throw if @user is not activated  set_flash_message(:notice, :success, :kind => "Facebook") if  is_navigational_format?  else  session["devise.facebook_data"] = request.env["omniauth.auth"]  redirect_to new_user_registration_url  end  end  def failure  redirect_to root_path  end  end  

<iframe> tag localhost can be use,but when I git push heroku 。

Posted: 06 Nov 2016 06:24 AM PST

iframe on heroku

page source code has ,web information without tag.

I use ruby on rails framework.

localhost

  • source code:

<iframe frameborder="no" border="0" marginwidth="0" marginheight="0" width=330 height=86 src="http://music.163.com/outchain/player?type=2&id=27534035&auto=1&height=66"></iframe>

rails generate devise:install error 1

Posted: 06 Nov 2016 05:56 AM PST

/home/leto/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/bundler-1.13.6/lib/bundler/runtime.rb:94:in rescue in block (2 levels) in require': There was an error while trying to load the gem 'uglifier'. Gem Load Error is: Could not find a JavaScript runtime. See https://github.com/rails/execjs for a list of available runtimes. Backtrace for gem load error is: /home/leto/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/execjs-2.7.0/lib/execjs/runtimes.rb:58:inautodetect' /home/leto/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/execjs-2.7.0/lib/execjs.rb:5:in <module:ExecJS>' /home/leto/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/execjs-2.7.0/lib/execjs.rb:4:in' /home/leto/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/uglifier-3.0.3/lib/uglifier.rb:5:in require' /home/leto/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/uglifier-3.0.3/lib/uglifier.rb:5:in' /home/leto/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/bundler-1.13.6/lib/bundler/runtime.rb:91:in require' /home/leto/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/bundler-1.13.6/lib/bundler/runtime.rb:91:inblock (2 levels) in require' /home/leto/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/bundler-1.13.6/lib/bundler/runtime.rb:86:in each' /home/leto/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/bundler-1.13.6/lib/bundler/runtime.rb:86:inblock in require' /home/leto/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/bundler-1.13.6/lib/bundler/runtime.rb:75:in each' /home/leto/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/bundler-1.13.6/lib/bundler/runtime.rb:75:inrequire' /home/leto/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/bundler-1.13.6/lib/bundler.rb:106:in require' /home/leto/rubyblog/config/application.rb:7:in' /home/leto/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/spring-2.0.0/lib/spring/application.rb:82:in require' /home/leto/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/spring-2.0.0/lib/spring/application.rb:82:inpreload' /home/leto/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/spring-2.0.0/lib/spring/application.rb:143:in serve' /home/leto/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/spring-2.0.0/lib/spring/application.rb:131:inblock in run' /home/leto/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/spring-2.0.0/lib/spring/application.rb:125:in loop' /home/leto/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/spring-2.0.0/lib/spring/application.rb:125:inrun' /home/leto/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/spring-2.0.0/lib/spring/application/boot.rb:19:in <top (required)>' /home/leto/.rbenv/versions/2.3.1/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:inrequire' /home/leto/.rbenv/versions/2.3.1/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in require' -e:1:in' Bundler Error Backtrace: (Bundler::GemRequireError) from /home/leto/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/bundler-1.13.6/lib/bundler/runtime.rb:90:in block (2 levels) in require' from /home/leto/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/bundler-1.13.6/lib/bundler/runtime.rb:86:ineach' from /home/leto/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/bundler-1.13.6/lib/bundler/runtime.rb:86:in block in require' from /home/leto/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/bundler-1.13.6/lib/bundler/runtime.rb:75:ineach' from /home/leto/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/bundler-1.13.6/lib/bundler/runtime.rb:75:in require' from /home/leto/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/bundler-1.13.6/lib/bundler.rb:106:inrequire' from /home/leto/rubyblog/config/application.rb:7:in <top (required)>' from /home/leto/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/spring-2.0.0/lib/spring/application.rb:82:inrequire' from /home/leto/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/spring-2.0.0/lib/spring/application.rb:82:in preload' from /home/leto/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/spring-2.0.0/lib/spring/application.rb:143:inserve' from /home/leto/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/spring-2.0.0/lib/spring/application.rb:131:in block in run' from /home/leto/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/spring-2.0.0/lib/spring/application.rb:125:inloop' from /home/leto/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/spring-2.0.0/lib/spring/application.rb:125:in run' from /home/leto/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/spring-2.0.0/lib/spring/application/boot.rb:19:in' from /home/leto/.rbenv/versions/2.3.1/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in require' from /home/leto/.rbenv/versions/2.3.1/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:inrequire' from -e:1:in `'

Pass plan_id into params with submit button

Posted: 06 Nov 2016 05:51 AM PST

I need to have two submit buttons in one form. The first one also needs to pass plan_id params with it. So basically I need this in my controller params "plan_id"=>"3". Right now my submit button looks like this <%= f.submit "Select", class: "button-big reverse-blue", name: "plan_id", value: "1" %> This does not work, if I put a pry in the create action I have no params for plan id. My question is this, how can I pass the plan_id of 1 into the form submit button?

HTML

      <label>          <%= f.submit "Select", class: "button-big reverse-blue", name: "plan_id", value: "1" %>        </label>  

Rails how to check which foreign key is found the most?

Posted: 06 Nov 2016 06:26 AM PST

I'm building an app in which players can buy shares of a company. The player that owns the most shares in a given company is the president. I have a company model that has a method check_president. This method is called whenever a share is bought or sold. I have a stock model that has an owner. the owner of the stock is stored through a owner_id foreign key. So for every stock there is a separate record with its own owner. What is the best way to check the president of the company?

something like this:

 def check_president      player_owned_stocks = ownstocks.where(ownable_type: "Player")      if(player_owned_stocks != nil)          *** find player with most stocks, if tied presidency should not change ***          president_id = player with most stocks      else          president_id = nil      end  end  

Automatic File Uploader to Website

Posted: 06 Nov 2016 06:15 AM PST

I have a website with a form to upload files. I want to automatically sign in and upload image files once a changes are scene on my local folder on my computer. Can any guidance be provided in the matter.

Extend the association of a has_many in Rails on the foreign key

Posted: 06 Nov 2016 05:30 AM PST

Bit of a strange one maybe. But the relationship between two models is optional. The User model has_many Deadlines, but the User field in the deadline is optional. If it's not filled, the deadline is a general one and is valid for all users.

In determining the has_many relationshop, I want to not only look for records in the Deadline model that have the user_id of the current object, but also for nil values.

I tried this, but it doesn't work:

  has_many :deadlines, -> (object){where(user: [nil, object.id])}, foreign_key:''  

The Query keeps including the original where and attaches the custom one as an AND, which does not result in the correct query.

SELECT "deadlines".* FROM "deadlines" WHERE "deadlines"."user_id" = $1 AND ("deadlines"."user_id" = 1 OR "deadlines"."user_id" IS NULL)  [["user_id", 1]]  

How can I make Rails return the correct Query:

SELECT "deadlines".* FROM "deadlines" WHERE ("deadlines"."user_id" = 1 OR "deadlines"."user_id" IS NULL)  

Jruby on Rails: assets precompile Java::JavaLang::OutOfMemoryError: GC overhead limit exceeded

Posted: 06 Nov 2016 04:55 AM PST

I am on jruby on rails technology. i am trying to precompile my assets but getting below error.

   Sass 3.5 will no longer support Ruby 1.9.3.      Please upgrade to Ruby 2.0.0 or greater as soon as possible.       rake aborted!      Java::JavaLang::OutOfMemoryError: GC overhead limit exceeded      org.mozilla.javascript.ScriptableObject.createSlot(org/mozilla/javascript/Script      ableObject.java:2913)      org.mozilla.javascript.ScriptableObject.getSlot(org/mozilla/javascript/Scriptabl      eObject.java:2841)      org.mozilla.javascript.ScriptableObject.putImpl(org/mozilla/javascript/Scriptabl      eObject.java:2725)      org.mozilla.javascript.ScriptableObject.put(org/mozilla/javascript/ScriptableObj      ect.java:515)      org.mozilla.javascript.IdScriptableObject.put(org/mozilla/javascript/IdScriptabl      eObject.java:386)      org.mozilla.javascript.ScriptableObject.defineProperty(org/mozilla/javascript/Sc      riptableObject.java:1613)      org.mozilla.javascript.NativeCall.<init>(org/mozilla/javascript/NativeCall.java:      65)      org.mozilla.javascript.ScriptRuntime.createFunctionActivation(org/mozilla/javasc      ript/ScriptRuntime.java:3374)      org.mozilla.javascript.Interpreter.initFrame(org/mozilla/javascript/Interpreter.      java:2753)      org.mozilla.javascript.Interpreter.interpretLoop(org/mozilla/javascript/Interpre      ter.java:1404)      org.mozilla.javascript.Interpreter.interpret(org/mozilla/javascript/Interpreter.      java:815)      org.mozilla.javascript.InterpretedFunction.call(org/mozilla/javascript/Interpret      edFunction.java:109)      org.mozilla.javascript.ContextFactory.doTopCall(org/mozilla/javascript/ContextFa      ctory.java:393)      org.mozilla.javascript.ScriptRuntime.doTopCall(org/mozilla/javascript/ScriptRunt      ime.java:3280)      org.mozilla.javascript.InterpretedFunction.call(org/mozilla/javascript/Interpret      edFunction.java:107)      Tasks: TOP => assets:precompile      (See full trace by running task with --trace)  

I have set jruby_opts env variable with value "-J-XX:MaxPermSize=2G" and JAVA_OPTS env variable with value "-XX:MaxPermSize=6024m" but still i am getting memeory issue.

What could be the reason for this ?? how i can resolve this issue?

Thanks,

Sorting on date which has nil values

Posted: 06 Nov 2016 04:36 AM PST

I'm trying to sort a model on a date, that also contains nil values. The attribute is for when the user was last seen. The problem is that when sorting on most recent seen Users, the nil values get displayed first. I explicitly don't want to set a default date.

Is there a way to show the nil values at the end of the result array instead of at the beginning when sorting of last_seen: :desc

Rails 5: add more parameters in strong parameter

Posted: 06 Nov 2016 04:28 AM PST

I'm using rails form_for for getting user input and creating data. At server side, I want to change some fields. For example: convert string "19/07/2016" to DateTime datatype or add some another custom fields.

When I receiving data from server, I use rails's strong parameter for filtering user's input. For example:

  params.require(:user).permit(:user_name, :password, :email)  

I see that those parameters put inside hash named ApplicationController:StrongParameter. Are there any easy way for add more parameters to this hash. (instead of browsing deep into this hash that make my code looks ugly).

Thanks

Hub and spoke postgresql database structure under Rails

Posted: 06 Nov 2016 04:10 AM PST

Context: an application with one cross-section of functionalities which involve many subjects and another with data that is exclusive to the individual subjects. In the attempt to distribute processing ressources and give the subjects ownership of their physical database, the goal is to create a central core of databases and N separate yet identically-structured (let's call them satellite) databases, one for each subject. Non of the satellite classes have_many classes of the cross-section functions.

So the question is for the following reasonings, is anything missing, is a particular pitfall lurking or am I in deep left field?

The first issue is providing connection data. Two approaches are envisaged:

1 - a long and tedious database.yml listing of database connections. Each subject has its unique identifier which would be used to name the satellite database, such as #{subject_id}_app_production. Then migrations would need to be aliased for each class that is exclusive to the subject.

2 - a deployment per subject, which then points to the relevant satellite server.
This appears to make more sense, as it avoids affecting migrations. It means the cross-section functionalities are structurally copied everywhere.

Then, all queries and operations for the "satellite" models will have to know that they are querying/writing to a specific database.
Under the first option, it is already predefined - but that means aliasing all satellite classes for each subject...
Under the second, the classes would be configured to know where to dialog with. Is that possible? and can it manage the unique identifiers of those tables distinctly?

How to make emails supplied in a Text Field formatted as an email link in Ruby on Rails

Posted: 06 Nov 2016 05:00 AM PST

I have a text based form, which enables anyone to type a text and at the same time supply an email in it and submit the form. Upon submitting the form, its show action should look like this image:

enter image description here

The code works for web links but emails supplied in the text are not formatted as email links and comes out as ordinary text. when I check the source code from browser, I see no html tag was added to the email supplied and it rendered as a text.

How can I supply html tag to the emails supplied in this text field such that the preview shows a real email address?

form.html.erb

<%= simple_form_for @job do |f| %><br>      <%= f.input :to_apply, label: 'How do people apply for this job?', placeholder: 'Example: Send a resume and cover letter to jobs@company.com.' %><br>  <% end %>  

model: job.rb

class Job < ActiveRecord::Base      validates :to_apply,              presence: true  end  

Arel, nested NamedFunction

Posted: 06 Nov 2016 03:00 AM PST

I'm trying to compose this query via Arel:

SELECT ROUND(UNIX_TIMESTAMP(CURTIME(4)) * 1000) FROM Table1

Using http://www.scuttle.io/ it return a simply:

Table1.select( Arel::Nodes::NamedFunction.new( 'ROUND', [ Arel::Nodes::NamedFunction.new( 'UNIX_TIMESTAMP', [Arel::Nodes::NamedFunction.new('CURTIME', [4])] ) * 1000 ] ) )

but my rails app doesn't accept it, giving me:

NoMethodError (undefined method `*' for #):

There's a way to fix it?

Trying to define two different values for same variable in one method. rails

Posted: 06 Nov 2016 03:50 AM PST

I am trying to define two different values to the one variable inside a sessions controller. I am getting "syntax error, unexpected keyword_elsif, expecting keyword_end" and "syntax error, unexpected end-of-input, expecting keyword_end". Obviously there is something wrong with elsif, or my grammar here. i am wondering what I am doing wrong or someone tell me a different way to do this.

def create    unless user = User.from_omniauth(env["omniauth.auth"])      user = User.find_by(email: params[:session][:email].downcase)    if user && user.authenticate(params[:session][:password])      log_in user      redirect_to user       # Log the user in and redirect to the user's show page.    else       # Create an error message.      flash.now[:danger] = 'Invalid email/password combination'      render 'new'    end       elsif       user = User.from_omniauth(env["omniauth.auth"])      log_in user      redirect_to user    end  end  

Resize image file_size uploaded via file_field | Paperclip

Posted: 06 Nov 2016 01:37 AM PST

I don't want to prevent large images from being uploaded. I just want it where when the image is saved to the DB it's saved in a smaller size.

For example if I'm showing the images as :medium then I want to resize the images to :medium dimensions so that the image will load faster.

pry(main)> Inspiration.last   id: 72,   user_id: 129,   name: "",   image_file_name: "ltc-cover-mon-fri.png",   image_content_type: "image/png",   image_file_size: 77198,   image_updated_at: Sun, 06 Nov 2016 04:20:00 EST -05:00>  

For example I tried resize in the model, but I get undefined method resize as an error so I guess resize isn't a built-in method.

class Inspiration < ActiveRecord::Base    has_attached_file :image, :styles => { :medium => "300x300>", :small => "150x150>" }    validates_attachment_content_type :image, :content_type => ["image/jpg", "image/jpeg", "image/png", "image/gif"]    before_save :resize      def resize      self.image = self.image.resize "300x300>"    end  end  

Redis Cached object don't respond to super. Issue in storing Active Record Object

Posted: 06 Nov 2016 01:46 AM PST

class User has 2 db column 'name' and 'lang' both string.

reader for lang is overridden as follows:

def lang    if false      puts 'something'    else      super    end  end  

Steps to reproduce

L1- In Terminal 1: Rails.cache.write('some_user', User.first)  L2- In Terminal 1: Rails.cache.read('some_user').name  L3- In Terminal 2: Rails.cache.read('some_user').name  L4- In Terminal 1: Rails.cache.read('some_user').lang  L5- In Terminal 2: Rails.cache.read('some_user').lang  

What is happening

L1- Writes into the cache store.  L2- name is a db column and hence the default reader returns 'gemin'.  L3- name is a db column and hence the default reader returns 'gemin'.  L4- lang's reader is overridden so it goes inside the new reader. super is called and it returns 'en'  L5- lang's reader is overridden so it goes inside the new reader. super is called and it breaks NoMethodError: super: no superclass method `lang' for #User:0x007fd71ca69078  

NOTE: If u reload! Terminal 1, same error will start to occur.

System configuration

Rails version: rails-5.0.0

Ruby version: 2.3.1

Change the discourse default landing page to a custom page

Posted: 06 Nov 2016 01:20 AM PST

I want to have a custom landing page for my Discourse website(those who don't know about Discourse, its a open source forum platform.More details at http://www.discourse.org/)

So when a user opens my website,instead of redirecting users to www.mydomain.com I want to redirect to www.mydomain.com/faq or www.mydomain.com/about.

Is there any way or setting to do this either through Discourse or Domain cpanel?

I am hosting my forum on AWS

How to use includes on rails 5.0 active record

Posted: 06 Nov 2016 01:59 AM PST

I'm tryin to get all data including has_many assosoation :

class Order < ApplicationRecord  has_many :order_details    class OrderDetail < ApplicationRecord  belongs_to :order  

Then retrive data using :

@orders = Order.includes(:order_details)  

But output it's only orders data :

[   {    id: 1,    customer_id: 4,    code: "c2306df9",    date: "2016-09-18T12:13:00.000Z",    downpayment: 500000,    amount: 3000000,    created_at: "2016-11-06T07:44:07.000Z",    updated_at: "2016-11-06T07:44:09.000Z"   }  ]  

My Expect is data order and order details merged. Is there any issue about includes on Rails 5.0 or may there is any another way ?

May Sameone can help. Thanks

how does rails as a server side language work?

Posted: 06 Nov 2016 01:09 AM PDT

I've been trying to figure out how rails is employed as a server side language but i can't seem to find any tutorials. I want to know what rails as a back-end scripting language looks like. Also do I need Ubuntu to work on rails as a back-end?

Etag is not working in Rails 5 anymore

Posted: 06 Nov 2016 12:55 AM PDT

I have caching with Etag enabled like so:

def index   if stale?(etag: @cards, last_modified: @cards.maximum(:updated_at))    ...   end  end  

It worked well with Rails 4 but does not anymore after upgrading to Rails 5 - always returns 200 OK and data.

Since I am using rack-cors, I have also allowed headers for CORS:

config.middleware.insert_before 0, Rack::Cors do    allow do      origins '*'      resource '*',      headers: :any,      methods: [:get, :post, :options, :patch, :delete],      expose: ['ETag', 'If-Last-Modified', 'If-None-Match', 'Last-Modified']    end  end  

And, here is a sample request I am sending (with Postman, terminal has same issue):

curl -X GET -H "If-None-Match: W/\"1fdad4ffa4c02482942831ba43bd2f54\"" -H "Cache-Control: no-cache" -H "Postman-Token: 60cb1bfa-5a0b-19ee-6959-8463ff7aaefe" "http://localhost:3000/api/v1/mobile/cards"  

thank you!

Rails 5: update action not working for AJAXified form

Posted: 06 Nov 2016 01:14 AM PST

I have view with dropdown where on user select, particular user role can be edited.

In /common/roles/index.erb I have this:

<form class="form-horizontal" role="form" method="get" action="/common/roles" data-remote="true">  <div class="col-sm-10"><select class="form-control m-b" name="users" id="user_list">    <option value="">Please, select user</option>       <% @users.each do |user| %>          <% for role in user.roles %>             <option value="<%= role.id %>"><%= user.name %></option>          <% end %>       <% end %>    </select>  </div>  </form>  <!-- Here we render role edit form -->  <div id = 'selected_role' ></div>  <!-- Role edit form ends -->  

This is select where User names are displayed, role ID are option value.

In roles.js I have listener where on select role ID is passed for Edit:

(function($) {  $('#user_list').change(function() {    var roleId = $( "#user_list" ).val();    $.ajax({      url: '/common/roles/' + roleId + '/edit',      method: 'GET',    });  });  })(jQuery); /*global jQuery*/  

/common/roles/edit.js.erb looks like this:

$("#selected_role").html("<%=j render 'common/roles/editrole', locals: { role: @role } %>");  

and it renders role edit partial /common/roles/_editrole.html.erb

<%= form_for :role, method: :patch, remote: true do |f| %>  <form class="m-t" role="form" action="" id="editrole">          <div class="form-group">              <label class="col-sm-2 control-label">General</label>              <div class="col-sm-10" >             <%= f.select :general, Role.generals.to_a.map { |w| [w[0].humanize, w[0]] }, {}, {class:"form-control m-b"} %>              </div>          </div>     <%= f.submit "Save changes", class: "btn btn-primary block full-width m-b" %>    <% end %>  </form>  

To complete this form I still need to finish with Update action where I have problem. On submitting role edit form it starts PATCH but with Edit url. Here's error in console:

Started PATCH "/common/roles/1/edit"  ActionController::RoutingError (No route matches [PATCH] "/common/roles/1/edit"):  

although particular PATCH route is in place:

common_role PATCH  /common/roles/:id(.:format)     common/roles#update  

In /common/roles_controller.rb I have this:

def update  @role = Role.find(params[:id])  if @role.update_attributes(role_params)  respond_to do |format|    format.html {                flash[:success] = "Role updated!"                redirect_to common_roles_path    }    format.js  

In /common/roles/update.js.erb I have this line: $('.editrole').hide()

I can open form in Edit and untill Update action it works fine. How do I fix Update action error, please?


Update for solution

In Update action I was missing respond_to do |format| + /common/roles/update.js.erb I had to change to this:

$('.container').empty().append('<%=    escape_javascript(      render 'update'    )  %>')  

How do I establish a relationship between two objects where both need to have many of the other object?

Posted: 06 Nov 2016 12:56 AM PDT

My situation is this: I have two models, Model (as in car model) and Engine. I have some Models which have more than one Engine (different model years came with different engines), and I have some Engines which belong to multiple different Models (single engine was reused across multiple models).

Forgive me for being (very) new to Rails and ActiveRecord, but this seems a bit more complicated than just a has_many and belongs_to. I could be wrong. I should also note that I'm using Rails 5.

Given that I already have my scaffolds/models in place and I'd rather not delete them, how do I write a migration to achieve the above situation? What do I need to add to the respective models?

Harmonic series in ruby

Posted: 06 Nov 2016 04:12 AM PST

I am trying to write a program that calculates a Harmonic series from a whole number. Such as 5 and calculates it until it reaches that argument.

So essentially it looks like:

h1: 1 / 1 h2: 3 / 2 h3: 11 / 6 h4: 25 / 12 h5: 137 / 60

This is what I have.

my question is, is there a formula that will calculate a harmonic series in ruby?

class Fraction    attr_accessor :numerator, :denominator    def initialize(n, d)  @numerator = n  @denominator = d  end    def +(rhs)  n = @numerator*rhs.denominator + @denominator*rhs.numerator  d = @denominator*rhs.denominator  n, d = reduce(n, d)  return Fraction.new(n, d)  end    def print  puts "#{@numerator} / #{@denominator}"  end    def reduce(n, d)  r = gcd(n, d)  return n / r, d / r  end    def gcd(a, b)  if a % b == 0  return b  else  return gcd(b, a % b)  end  end  private :reduce, :gcd  end  

Rails server unexpected token parse error for JSON request

Posted: 05 Nov 2016 11:36 PM PDT

I am sending this curl request to my rails server

curl -XPOST 'http://localhost:3000/articles' -H "Content-Type: application/json" --data-binary @body  

the contents of request body is this json object

{'article': {'content': [u'\u0639\u0645\u0627\u0646 \u2013 \u0623\u0639\u0644\u0646 \u0645\u062d\u0627\u0641\u0638 \u0627\u0644\u0628\u0646\u0643 \u0627\u0644\u0645\u0631\u0643\u0632\u064a \u0627\u0644\u062f\u0643\u062a\u0648\u0631 \u0632\u064a\u0627\u062f \u0641\u0631\u064a\u0632 \u0639\u0646 \u0627\u0637\u0644\u0627\u0642 \u0646\u0638\u0627\u0645 \u063a\u0631\u0641\u0629 \u0627\u0644\u062a\u0642\u0627\u0635 \u0627\u0644\u0622\u0644\u064a (ACH) \u0627\u0644\u062c\u062f\u064a\u062f \u0648\u0627\u0644\u0630\u064a \u064a\u062a\u0648\u0627\u0641\u0642 \u0645\u0639 \u0627\u0644\u0645\u0639\u064a\u0627\u0631 \u0627\u0644\u062c\u062f\u064a\u062f (ISO 20022)\u060c \u0648\u0628\u062f\u0623 \u062a\u0634\u063a\u064a\u0644\u0647 \u0627\u0644\u0641\u0639\u0644\u064a \u0646\u0647\u0627\u064a\u0629 \u0627\u0644\u0634\u0647\u0631 \u0627\u0644\u0645\u0627\u0636\u064a.',                       u'\n\u0648\u0642\u0627\u0644 \u0641\u0631\u064a\u0632 \u0641\u064a \u0628\u064a\u0627\u0646 \u0623\u0635\u062f\u0631\u0647 \u0627\u0644\u0628\u0646\u0643 \u0627\u0645\u0633 \u0627\u0646 \u0627\u0644\u0646\u0638\u0627\u0645 \u064a\u0639\u062f \u064a627\u0644\u0642\u0627\u0639\u062f\u0629 \u0627\u0644\u0623\u0633\u0627\u0633\u064a\u0629 \u0644\u062a\u0642\u0627\u0635 \u0627\u0644\u062d\u0648\u0627\u0644\u0627\u062a \u0627\u0644\u0645\u0627\u0644\u064a\u0629 \u0635\u063a\u064a\u0631\u0629 \u0648\u0645\u062a\u0648\u0633\u0637\u0629 \u0627\u0644\u062d\u062c\u0645 \u0628\u0639\u0645\u0644\u0627\u062a \u0645\u062e\u062a\u0644\u0641\u0629 \u0641\u064a \u0627\u0644\u0623\u0631\u062f\u0646\u060c \u0627\u0644\u0623\u0645\u0631 \u0627\u0644\u0630\u064a \u064a\u0633\u0647\u0644 \u0639\u0645\u0644\u064a\u0629 \u062a\u0642\u0627\u0635 \u0627\u0644\u0623\u0645\u0648\u0627\u0644 \u0628\u064a\u0646 \u0627\u0644\u0628\u0646\u0648\u0643 \u0627\u0644\u0639\u0627\u0645\u0644\u0629 \u0641\u064a \u0627\u0644\u0645\u0645\u0644\u0643\u0629 \u0648\u064a\u0642\u0644\u0644 \u0627\u0644\u0627\u0639\u062a\u0645\u0627\u062f \u0639\u0644\u0649 \u0627\u0644\u0646\u0642\u062f \u0627\u0644\u0648\u0631\u0642\u064a \u0648\u0627\u0644\u0634\u064a\u0643\u0627\u062a \u0641\u064a \u0639\u0645\u0644\u064a\u0627\u062a \u0627\u0644\u062f\u0641\u0639.',                       u'\u0648\u0623\u0636\u0627\u0641 \u0627\u0646 \u0627\u0644\u0646\u0638\u0627\u0645 \u064a\u0639\u0645\u0644 \u0639\u0644\u0649 \u062a\u0634\u062c\u064a\u0639 \u0627\u0644\u0645\u0648\u0627\u0637\u0646\u064a\u0646 \u0639\u0644\u0649 \u0627\u0644\u0627\u0639\u062a\u0645\u0627\u062f \u0639\u0644\u0649 \u0627\u0644\u062a\u062d\u0648\u064a\u0644\u0627\u062a \u0627\u0644\u0627\u0644\u0643\u062a\u0631\u0648\u0646\u064a\u0629 \u0644\u062a\u062d\u0648\u064a\u0644 \u0627\u0644\u0627\u0645\u0648\u0627\u0644\u060c \u0646\u0638\u0631\u0627 \u0644\u0633\u0631\u0639\u062a\u0647\u0627 \u0648\u0627\u0646\u062e\u0641\u0627\u0636 \u062a\u0643\u0644\u0641\u062a\u0647\u0627 \u0645\u0642\u0627\u0631\u0646\u0629 \u0628\u062a\u0643\u0644\u0641\u0629 \u0623\u062f\u0648\u0627\u062a \u0627\u0644\u062f\u0641\u0639 \u0627\u0644\u0645\u062a\u0627\u062d\u0629 \u062d\u0627\u0644\u064a\u0627\u060c \u0628\u0645\u0627 \u064a\u0639\u0632\u0632 \u0627\u0644\u0643\u0641\u0627\u0621\u0629 \u0627\u0644\u0627\u0642\u062a\u0635\u0627\u062f\u064a\u0629 \u0648\u0627\u062f\u0627\u0631\u0629 \u0627\u0644\u0633\u064a\u0648\u0644\u0629 \u0627\u0644\u0646\u0642\u062f\u064a\u0629 \u0644\u062f\u0649 \u0627\u0644\u0628\u0646\u0648\u0643\u060c \u0648\u062a\u0633\u0647\u064a\u0644 \u062f\u0648\u0631\u0629 \u0627\u0644\u0646\u0642\u0648\u062f \u0641\u064a \u0627\u0644\u0627\u0642\u062a\u0635\u0627\u062f \u0628\u0637\u0631\u064a\u0642\u0629 \u0622\u0645\u0646\u0629 \u0648\u0641\u0639\u0627\u0644\u0629\u060c \u0645\u0627 \u064a\u062f\u0639\u0645 \u062a\u0646\u0641\u064a\u0630 \u0627\u0644\u0633\u064a\u0627\u0633\u0629 \u0627\u0644\u0646\u0642\u062f\u064a\u0629 \u0648\u064a\u0633\u0627\u0647\u0645 \u0641\u064a \u062a\u062d\u0642\u064a\u0642 \u0627\u0644\u0627\u0633\u062a\u0642\u0631\u0627\u0631 \u0627\u0644\u0645\u0627\u0644\u064a \u0641\u064a \u0627\u0644\u0645\u0645\u0644\u0643\u0629.',                       u'\n\u0648\u0627\u0648\u0636\u062d \u0623\u0646 \u0627\u0633\u062a\u062e\u062f\u0627\u0645 \u0627\u0644\u0645\u0639\u064a\u0627\u0631 (ISO 20022) \u0641\u064a \u0639\u0645\u0644\u064a\u0627\u062a \u0627\u0644\u062a\u062d\u0648\u064a\u0644 \u0636\u0645\u0646 \u0627\u0644\u0646\u0638\u0627\u0645 \u0627\u0644\u062c\u062f\u064a\u062f \u0633\u064a\u0633\u0627\u0647\u0645 \u0641\u064a \u062a\u0633\u0647\u064a\u0644 \u0627\u0644\u062a\u0639\u0627\u0645\u0644 \u0645\u0639 \u0623\u064a\u0629 \u062a\u0637\u0648\u0631\u0627\u062a \u0645\u0633\u062a\u0642\u0628\u0644\u064a\u0629 \u0641\u064a \u0627\u0644\u0627\u0633\u0648\u0627\u0642 \u0627\u0644\u0645\u0627\u0644\u064a\u0629 \u0648\u0623\u0646\u0638\u0645\u0629 \u0627\u0644\u062f\u0641\u0639 \u0641\u064a \u0627\u0644\u0645\u0645\u0644\u0643\u0629\u060c \u0645\u0624\u0643\u062f\u0627 \u0623\u0647\u0645\u064a\u0629 \u0648\u062c\u0648\u062f \u0628\u0646\u064a\u0629 \u0645\u062a\u0643\u0627\u0645\u0644\u0629 \u0648\u0641\u0642 \u0627\u0644\u0645\u0639\u0627\u064a\u064a\u0631 \u0627\u0644\u0639\u0627\u0644\u0645\u064a\u0629 \u0648\u0627\u0644\u0645\u0645\u0627\u0631\u0633\u0627\u062a \u0627\u0644\u0641\u0636\u0644\u0649\u060c \u0627\u0644\u062a\u064a \u064a\u0648\u0641\u0631\u0647\u0627 \u0627\u0644\u0646\u0638\u0627\u0645 \u0627\u0644\u062c\u062f\u064a\u062f \u0644\u0644\u0628\u0646\u0648\u0643 \u0648\u0627\u0644\u0645\u0624\u0633\u0633\u0627\u062a \u0627\u0644\u0645\u0627\u0644\u064a\u0629\u060c \u0648\u0627\u0644\u0630\u064a \u0633\u064a\u0633\u0627\u0647\u0645 \u0641\u064a \u062a\u0639\u0632\u064a\u0632 \u0645\u0635\u062f\u0627\u0642\u064a\u0629 \u0627\u0644\u062a\u062d\u0648\u064a\u0644\u0627\u062a \u0627\u0644\u0645\u0627\u0644\u064a\u0629 \u0628\u064a\u0646 \u0627\u0644\u0628\u0646\u0648\u0643.',                       u'\n\u0648\u0623\u0636\u0627\u0641 \u0641\u0631\u064a\u0632\u060c \u0627\u0646 \u0627\u0644\u0645\u0646\u0638\u0648\u0645\u0629 \u0627\u0644\u062c\u062f\u064a\u062f\u0629 \u0642\u0627\u062f\u0631\u0629 \u0639\u0644\u0649 \u0645\u0639\u0627\u0644\u062c\u0629 \u062a\u0642\u0627\u0635 \u0627\u0644\u062a\u062d\u0648\u064a\u0644\u0627\u062a \u0627\u0644\u062f\u0627\u0626\u0646\u0629 \u0648\u0627\u0644\u0645\u062f\u064a\u0646\u0629 \u0628\u062c\u0645\u064a\u0639 \u0623\u0646\u0648\u0627\u0639\u0647\u0627\u060c \u0645\u0627 \u0633\u064a\u0648\u0641\u0631 \u062e\u064a\u0627\u0631\u0627\u062a \u0645\u062a\u0639\u062f\u062f\u0629 \u0644\u0644\u0628\u0646\u0648\u0643 \u0648\u0627\u0644\u0623\u0641\u0631\u0627\u062f \u0648\u0627\u0644\u0634\u0631\u0643\u0627\u062a.',                       u'\n\u0648\u064a\u0636\u0645\u0646 \u0627\u0644\u0646\u0638\u0627\u0645 \u0627\u0644\u062c\u062f\u064a\u062f \u0627\u0644\u062a\u062d\u0648\u064a\u0644\u0627\u062a \u0627\u0644\u062a\u0648\u0627\u0641\u0642\u064a\u0629 \u0641\u064a \u0627\u0644\u062a\u062d\u0648\u064a\u0644\u0627\u062a \u0636\u0645\u0646 \u0628\u064a\u0626\u0627\u062a \u0639\u0645\u0644 \u0645\u062e\u062a\u0644\u0641\u0629\u060c \u0628\u0645\u0627 \u064a\u062f\u0639\u0645 \u0627\u0628\u062a\u0643\u0627\u0631 \u0627\u0644\u0645\u0646\u062a\u062c\u0627\u062a \u0648\u0627\u0644\u062e\u062f\u0645\u0627\u062a \u0627\u0644\u0645\u0627\u0644\u064a\u0629\u060c \u0648\u064a\u0642\u062f\u0645 \u0641\u0648\u0627\u0626\u062f \u0648\u0627\u0636\u062d\u0629 \u0644\u0644\u0627\u0642\u062a\u0635\u0627\u062f \u0627\u0644\u0623\u0631\u062f\u0646\u064a.',                       u'\n\u0648\u0628\u064a\u0646 \u0627\u0644\u062f\u0643\u062a\u0648\u0631 \u0641\u0631\u064a\u0632 \u0623\u0646 \u0627\u0644\u0646\u0638\u0627\u0645 \u0633\u064a\u062d\u0642\u0642 \u0648\u0641\u0648\u0631\u0627\u062a \u0643\u0628\u064a\u0631\u0629 \u0639\u0644\u0649 \u0642\u0637\u0627\u0639 \u0627\u0644\u0634\u0631\u0643\u0627\u062a \u0648\u0627\u0644\u0645\u0624\u0633\u0633\u0627\u062a \u0627\u0644\u062d\u0643\u0648\u0645\u064a\u0629 \u0641\u064a \u0625\u0631\u0633\u0627\u0644 \u0645\u0644\u0641\u0627\u062a \u0627\u0644\u062a\u062d\u0648\u064a\u0644\u0627\u062a \u0645\u062a\u0639\u062f\u062f\u0629 \u0627\u0644\u0645\u0633\u062a\u0641\u064a\u062f\u064a\u0646 \u0645\u062b\u0644 \u0645\u0644\u0641\u0627\u062a \u0627\u0644\u0631\u0648\u0627\u062a\u0628\u060c \u0648\u0623\u0631\u0628\u0627\u062d \u0627\u0644\u0623\u0633\u0647\u0645\u060c \u0648\u0631\u0648\u0627\u062a\u0628 \u0627\u0644\u0636\u0645\u0627\u0646 \u0627\u0644\u0627\u062c\u062a\u0645\u0627\u0639\u064a \u0648\u063a\u064a\u0631\u0647\u0627 \u0645\u0646 \u0627\u0644\u062f\u0641\u0639\u0627\u062a\u060c \u0639\u0644\u0627\u0648\u0629 \u0639\u0644\u0649 \u062a\u0641\u0627\u0648\u064a\u0636 \u0627\u0644\u0642\u064a\u062f \u0639\u0644\u0649 \u0627\u0644\u062d\u0633\u0627\u0628\u0627\u062a \u0644\u0644\u0648\u0641\u0627\u0621 \u0628\u0627\u0644\u062a\u0632\u0627\u0645\u0627\u062a \u0627\u0644\u062f\u0641\u0639\u0627\u062a \u0627\u0644\u0645\u062c\u062f\u0648\u0644\u0629 \u0628\u0622\u062c\u0627\u0644 \u0645\u062e\u062a\u0644\u0641\u0629 \u0628\u0633\u0647\u0648\u0644\u0629 \u0648\u064a\u0633\u0631 \u0648\u0636\u0645\u0646 \u0628\u064a\u0626\u0629 \u0622\u0645\u0646\u0629.',                       u'\n\u0628\u062f\u0648\u0631\u0647\u060c \u0628\u064a\u0646 \u0645\u062f\u064a\u0631 \u0627\u0644\u0645\u0634\u0631\u0648\u0639 \u0645\u0643\u0633\u064a\u0645 \u0628\u0648\u0631\u064a\u0633\u064a\u0646\u0643\u0648\u060c \u0645\u0646 \u0634\u0631\u0643\u0629\xa0 CMA\u060c \u0623\u0646 \u0647\u0630\u0627 \u0627\u0644\u0646\u0638\u0627\u0645 \u064a\u0648\u0641\u0631 \u0641\u0648\u0627\u0626\u062f \u0648\u0645\u0632\u0627\u064a\u0627 \u0625\u0636\u0627\u0641\u064a\u0629 \u0639\u0632\u0632\u062a \u0639\u0645\u0644\u064a\u0627\u062a \u0627\u0644\u062a\u062d\u0648\u064a\u0644 \u0641\u064a \u0623\u0646\u0638\u0645\u0629 \u0627\u0644\u062f\u0641\u0639 \u0641\u064a \u0627\u0644\u0623\u0631\u062f\u0646\u060c \u0648\u0645\u0646\u0647\u0627 \u062e\u0635\u0627\u0626\u0635 \u0645\u062a\u0627\u0628\u0639\u0629 \u0648\u0645\u0631\u0627\u0642\u0628\u0629 \u0645\u062a\u0645\u064a\u0632\u0629 \u0644\u0639\u0645\u0644\u064a\u0627\u062a \u0627\u0644\u062a\u062d\u0648\u064a\u0644 \u0627\u0644\u0645\u0627\u0644\u064a \u0641\u064a \u0627\u0644\u0628\u0646\u0643 \u0627\u0644\u0645\u0631\u0643\u0632\u064a \u0648\u0627\u0644\u0628\u0646\u0648\u0643 \u0627\u0644\u062a\u062c\u0627\u0631\u064a\u0629\u060c \u0648\u0648\u0638\u0627\u0626\u0641 \u062e\u0627\u0635\u0629 \u0628\u0625\u062f\u0627\u0631\u0629 \u0627\u0644\u0633\u064a\u0648\u0644\u0629 \u0648\u0627\u062f\u0627\u0631\u0629 \u0627\u0644\u0645\u062e\u0627\u0637\u0631 \u0627\u0644\u0645\u0635\u0627\u062d\u0628\u0629 \u0644\u0647\u0627\u060c \u0648\u0628\u0646\u064a\u0629 \u062a\u062d\u062a\u064a\u0629 \u062a\u0645\u062a\u0627\u0632 \u0628\u0627\u0644\u0633\u0631\u0639\u0629 \u0648\u0627\u0644\u0641\u0639\u0627\u0644\u064a\u0629 \u0648\u0627\u0644\u0623\u0645\u0627\u0646 \u0628\u0645\u0627 \u064a\u0633\u0647\u0644 \u0645\u0648\u0627\u0643\u0628\u0629 \u0627\u0644\u062a\u0637\u0648\u0631\u0627\u062a \u0627\u0644\u062a\u0642\u0646\u064a\u0629 \u0648\u0627\u0644\u0645\u0627\u0644\u064a\u0629 \u0641\u064a \u0627\u0644\u0623\u0633\u0648\u0627\u0642 \u0627\u0644\u0645\u0627\u0644\u064a\u0629.',                       u'\n\u064a\u0630\u0643\u0631 \u0623\u0646 \u0627\u0644\u0646\u0638\u0627\u0645 \u0627\u0644\u062c\u062f\u064a\u062f \u064a\u062e\u062f\u0645 \u062c\u0645\u064a\u0639 \u0627\u0644\u0628\u0646\u0648\u0643 \u0627\u0644\u0639\u0627\u0645\u0644\u0629 \u0641\u064a \u0627\u0644\u0645\u0645\u0644\u0643\u0629 \u0645\u0646 \u062e\u0644\u0627\u0644 \u0627\u0644\u0634\u0628\u0643\u0629 \u0627\u0644\u0622\u0645\u0646\u0629 \u0644\u0644\u0628\u0646\u0648\u0643 VPN \u0643\u0634\u0628\u0643\u0629 \u0631\u0626\u064a\u0633\u064a\u0629 \u0644\u062a\u0646\u0627\u0642\u0644 \u0627\u0644\u062a\u062d\u0648\u064a\u0644\u0627\u062a \u0627\u0644\u0645\u0627\u0644\u064a\u0629\u060c \u0643\u0645\u0627 \u064a\u0645\u0643\u0646 \u0627\u0633\u062a\u062e\u062f\u0627\u0645 \u0634\u0628\u0643\u0629 \u0633\u0648\u064a\u0641\u062a \u0628\u064a\u0646 \u0627\u0644\u0628\u0646\u0648\u0643 \u0643\u0634\u0628\u0643\u0629 \u0628\u062f\u064a\u0644\u0629.',                       u'\n\u0648\u0623\u0639\u0631\u0628 \u0641\u0631\u064a\u0632 \u0648\u0641\u0631\u064a\u0642 \u0645\u0634\u0631\u0648\u0639 \u062a\u0637\u0628\u064a\u0642 \u0627\u0644\u0646\u0638\u0627\u0645 \u0639\u0646 \u0634\u0643\u0631\u0647\u0645 \u0644\u0644\u0642\u0637\u0627\u0639 \u0627\u0644\u0645\u0635\u0631\u0641\u064a \u0648\u0634\u0631\u0643\u0629 CMA \u0648\u062c\u0645\u0639\u064a\u0629 SWIFT \u0644\u062a\u0639\u0627\u0648\u0646\u0647\u0645 \u0627\u0644\u0645\u062b\u0645\u0631 \u0641\u064a \u0625\u0646\u062c\u0627\u062d \u0647\u0630\u0627 \u0627\u0644\u0645\u0634\u0631\u0648\u0639 \u0627\u0644\u0648\u0637\u0646\u064a \u0627\u0644\u0645\u0647\u0645 \u0627\u0644\u0630\u064a \u062c\u0627\u0621 \u0644\u064a\u062a\u0645\u0645 \u0627\u0633\u062a\u0631\u0627\u062a\u064a\u062c\u064a\u0629 \u0627\u0644\u0628\u0646\u0643 \u0627\u0644\u0645\u0631\u0643\u0632\u064a \u0627\u0644\u0623\u0631\u062f\u0646\u064a \u0644\u0644\u0623\u0639\u0648\u0627\u0645 2013-2016 \u0627\u0644\u0647\u0627\u062f\u0641\u0629 \u0625\u0644\u0649 \u0625\u0639\u0627\u062f\u0629 \u0647\u064a\u0643\u0644\u0629 \u0648\u062a\u0637\u0648\u064a\u0631 \u0627\u0644\u0628\u0646\u064a\u0629 \u0627\u0644\u062a\u062d\u062a\u064a\u0629 \u0644\u0623\u0646\u0638\u0645\u0629 \u0627\u0644\u062f\u0641\u0639 \u0648\u0627\u0644\u062a\u062d\u0648\u064a\u0644 \u0641\u064a \u0627\u0644\u0645\u0645\u0644\u0643\u0629 \u0627\u0646\u062a\u0642\u0627\u0644\u0627 \u0645\u0646 \u0628\u064a\u0626\u0629 \u0627\u0644\u062f\u0641\u0639 \u0627\u0644\u0648\u0631\u0642\u064a \u0625\u0644\u0649 \u0628\u064a\u0626\u0629 \u0627\u0644\u062f\u0641\u0639 \u0627\u0644\u0625\u0644\u0643\u062a\u0631\u0648\u0646\u064a\u0629 \u0644\u062e\u062f\u0645\u0629 \u0627\u0644\u0645\u0648\u0627\u0637\u0646\u064a\u0646 \u0648\u0643\u0627\u0641\u0629 \u0627\u0644\u0641\u0639\u0627\u0644\u064a\u0627\u062a \u0627\u0644\u0627\u0642\u062a\u0635\u0627\u062f\u064a\u0629 \u0641\u064a \u0627\u0644\u0645\u0645\u0644\u0643\u0629.',                       u'\n\u0648\u0630\u0643\u0631 \u0623\u0646 \u0627\u0644\u0628\u0646\u0643 \u0627\u0644\u0645\u0631\u0643\u0632\u064a \u0637\u0648\u0631 \u0641\u064a \u0639\u0627\u0645 2014 \u0628\u0646\u064a\u0629 \u062a\u062d\u062a\u064a\u0629 \u0645\u0646 \u062e\u0648\u0627\u062f\u0645 \u0648\u0634\u0628\u0643\u0627\u062a \u0628\u062a\u0643\u0646\u0648\u0644\u0648\u062c\u064a\u0627 \u062d\u062f\u064a\u062b\u0629 \u0648\u0645\u062a\u0637\u0648\u0631\u0629 \u062a\u062e\u062f\u0645 \u0623\u0646\u0638\u0645\u0629 \u0627\u0644\u062f\u0641\u0639 \u0648\u0627\u0644\u062a\u062d\u0648\u064a\u0644 \u0627\u0644\u0625\u0644\u0643\u062a\u0631\u0648\u0646\u064a \u0645\u0646 \u062b\u0644\u0627\u062b\u0629 \u0645\u0648\u0627\u0642\u0639\u060c \u0631\u0626\u064a\u0633\u064a\u0629 \u0648\u062b\u0627\u0646\u0648\u064a\u0629 \u0648\u0645\u0648\u0642\u0639 \u062a\u0639\u0627\u0641\u064a \u0645\u0646 \u0627\u0644\u0645\u062e\u0627\u0637\u0631 \u0648\u0636\u0645\u0646 \u0623\u0643\u062b\u0631 \u0645\u0646 \u0634\u0628\u0643\u0629 \u0622\u0645\u0646\u0629 \u062a\u0631\u0628\u0637 \u0627\u0644\u0628\u0646\u0643 \u0627\u0644\u0645\u0631\u0643\u0632\u064a \u0645\u0639 \u0643\u0627\u0641\u0629 \u0628\u0646\u0648\u0643 \u0627\u0644\u0645\u0645\u0644\u0643\u0629 \u0641\u064a \u0627\u0644\u0645\u0648\u0627\u0642\u0639 \u0627\u0644\u062b\u0644\u0627\u062b\u0629.',                       u'\n\u0648\u0643\u0627\u0646 \u0627\u0644\u0628\u0646\u0643 \u0627\u0644\u0645\u0631\u0643\u0632\u064a \u0623\u0637\u0644\u0642 \u0646\u0638\u0627\u0645 \u0639\u0631\u0636 \u0648\u062a\u062d\u0635\u064a\u0644 \u0627\u0644\u0641\u0648\u0627\u062a\u064a\u0631 \u0625\u0644\u0643\u062a\u0631\u0648\u0646\u064a\u0627 -\u0625\u064a \u0641\u0648\u0627\u062a\u064a\u0631\u0643\u0645- \u0641\u064a \u0645\u0646\u062a\u0635\u0641 \u0639\u0627\u0645 2014\u060c \u062a\u0644\u0627\u0647 \u0625\u0637\u0644\u0627\u0642 \u0646\u0638\u0627\u0645 \u0627\u0644\u062a\u0633\u0648\u064a\u0627\u062a \u0627\u0644\u0625\u062c\u0645\u0627\u0644\u064a\u0629 \u0627\u0644\u0641\u0648\u0631\u064a RTGS \u0628\u0623\u0639\u0644\u0649 \u0627\u0644\u0645\u0648\u0627\u0635\u0641\u0627\u062a \u0627\u0644\u0639\u0627\u0644\u0645\u064a\u0629 \u0641\u064a \u0627\u0644\u0631\u0628\u0639 \u0627\u0644\u0623\u0648\u0644 \u0645\u0646 \u0639\u0627\u0645 2015\u060c \u0628\u0627\u0644\u062a\u0632\u0627\u0645\u0646 \u0645\u0639 \u0625\u0637\u0644\u0627\u0642 \u0627\u0644\u0645\u0631\u062d\u0644\u0629 \u0627\u0644\u0623\u0648\u0644\u0649 \u0644\u0646\u0638\u0627\u0645 \u0627\u0644\u062e\u062f\u0645\u0627\u062a \u0627\u0644\u0645\u0635\u0631\u0641\u064a\u0629 \u0627\u0644\u0625\u0644\u0643\u062a\u0631\u0648\u0646\u064a\u0629 \u0644\u0639\u0645\u0644\u0627\u0621 \u0627\u0644\u0628\u0646\u0643 \u0627\u0644\u0645\u0631\u0643\u0632\u064a \u0645\u0646 \u0627\u0644\u0645\u0624\u0633\u0633\u0627\u062a \u0627\u0644\u062d\u0643\u0648\u0645\u064a\u0629 e-banking\u060c \u062b\u0645 \u0625\u0637\u0644\u0627\u0642 \u0646\u0638\u0627\u0645 \u0627\u0644\u062f\u0641\u0639 \u0628\u0627\u0644\u0647\u0627\u062a\u0641 \u0627\u0644\u0646\u0642\u0627\u0644 JoMoPay.',                       u'\n\u0648\u0641\u064a \u0627\u0644\u0631\u0628\u0639 \u0627\u0644\u0623\u0648\u0644 \u0645\u0646 \u0639\u0627\u0645 2016\u060c \u062a\u0645 \u0625\u0637\u0644\u0627\u0642 \u0627\u0644\u0646\u0638\u0627\u0645 \u0627\u0644\u0645\u0624\u062a\u0645\u062a \u0644\u0625\u062f\u0627\u0631\u0629 \u0627\u0644\u062f\u064a\u0646 \u0627\u0644\u0639\u0627\u0645 \u0627\u0644\u0645\u062d\u0644\u064a\u060c \u0648\u0645\u0646\u0647\u0627 \u0625\u062f\u0627\u0631\u0629 \u0645\u0632\u0627\u062f\u0627\u062a \u0627\u0644\u0633\u0646\u062f\u0627\u062a \u0627\u0644\u062d\u0643\u0648\u0645\u064a\u0629\u060c \u0648\u0627\u0644\u062a\u062f\u0627\u0648\u0644 \u0628\u0627\u0644\u0633\u0646\u062f\u0627\u062a \u0627\u0644\u062d\u0643\u0648\u0645\u064a\u0629 \u0648\u062e\u062f\u0645\u0629 \u0627\u0644\u062f\u064a\u0646 \u0627\u0644\u0639\u0627\u0645\u060c \u0648\u0633\u0646\u062f\u0627\u062a \u0627\u0644\u0623\u0641\u0631\u0627\u062f Depo/X .',                       u'\n\u0648\u0641\u064a \u0633\u064a\u0627\u0642 \u0645\u062a\u0635\u0644\u060c \u0639\u0642\u062f\u062a \u0627\u0644\u0644\u062c\u0646\u0629 \u0627\u0644\u062a\u0648\u062c\u064a\u0647\u064a\u0629 \u0627\u0644\u0639\u0644\u064a\u0627 \u0644\u062a\u0642\u0646\u064a\u0629 \u0627\u0644\u0645\u0639\u0644\u0648\u0645\u0627\u062a \u0641\u064a \u0627\u0644\u0628\u0646\u0643 \u0627\u0644\u0645\u0631\u0643\u0632\u064a \u0628\u0631\u0626\u0627\u0633\u0629 \u0641\u0631\u064a\u0632 \u0648\u0646\u0627\u0626\u0628\u064a \u0627\u0644\u0645\u062d\u0627\u0641\u0638\u060c \u0648\u0645\u062f\u0631\u0627\u0621 \u062f\u0648\u0627\u0626\u0631 \u0627\u0644\u0628\u0646\u0643 \u0627\u0644\u0645\u0631\u0643\u0632\u064a \u0627\u062c\u062a\u0645\u0627\u0639\u0627 \u0644\u0645\u0646\u0627\u0642\u0634\u0629 \u0627\u0644\u0645\u0634\u0631\u0648\u0639\u0627\u062a \u0627\u0644\u0645\u0637\u0631\u0648\u062d\u0629 \u0639\u0644\u0649 \u0627\u0644\u062e\u0637\u0629 \u0627\u0644\u0627\u0633\u062a\u0631\u0627\u062a\u064a\u062c\u064a\u0629 \u0644\u0644\u0641\u062a\u0631\u0629 \u0627\u0644\u0645\u0642\u0628\u0644\u0629 \u0644\u0627\u0633\u062a\u0643\u0645\u0627\u0644 \u0645\u0633\u064a\u0631\u0629 \u0627\u0644\u062a\u0637\u0648\u064a\u0631 \u0648\u0627\u0644\u0623\u062a\u0645\u062a\u0629 \u0644\u0644\u0623\u0639\u0645\u0627\u0644 \u0627\u0644\u0645\u0627\u0644\u064a\u0629 \u0648\u0627\u0644\u0645\u0635\u0631\u0641\u064a\u0629\u060c \u0648\u0633\u064a\u0639\u0644\u0646 \u0627\u0644\u0628\u0646\u0643 \u0639\u0646 \u0627\u0633\u062a\u0631\u0627\u062a\u064a\u062c\u064a\u062a\u0647 \u0627\u0644\u0645\u0642\u0628\u0644\u0629 \u0641\u064a \u062d\u064a\u0646\u0647 \u0628\u0639\u062f \u0627\u0644\u062a\u0634\u0627\u0648\u0631 \u062d\u0648\u0644\u0647\u0627 \u0645\u0639 \u0634\u0631\u0643\u0627\u0626\u0647 \u0627\u0644\u0627\u0633\u062a\u0631\u0627\u062a\u064a\u062c\u064a\u064a\u0646. (\u0628\u062a\u0631\u0627)'],           'headline': [u'\xab\u0627\u0644\u0645\u0631\u0643\u0632\u064a\xbb \u064a\u0637\u0644\u0642 \u0646\u0638\u0627\u0645\u0627 \u062c\u062f\u064a\u062f\u0627 \u0644\u0644\u062a\u062d\u0648\u064a\u0644 \u0627\u0644\u0625\u0644\u0643\u062a\u0631\u0648\u0646\u064a \u0648\u0627\u0644\u062a\u0642\u0627\u0635']}}  

And this is my articles controller which i simply generated through scaffolding

class ArticlesController < ApplicationController    before_action :set_article, only: [:show, :edit, :update, :destroy]      def index      @articles = Article.all    end      def show    end      def new      @article = Article.new    end        def edit    end      def create      @article = Article.new(article_params)        respond_to do |format|        if @article.save          format.html { redirect_to @article, notice: 'Article was successfully created.' }          format.json { render :show, status: :created, location: @article }        else          format.html { render :new }          format.json { render json: @article.errors, status: :unprocessable_entity }        end      end    end      def update      respond_to do |format|        if @article.update(article_params)          format.html { redirect_to @article, notice: 'Article was successfully updated.' }          format.json { render :show, status: :ok, location: @article }        else          format.html { render :edit }          format.json { render json: @article.errors, status: :unprocessable_entity }        end      end    end      def destroy      @article.destroy      respond_to do |format|        format.html { redirect_to articles_url, notice: 'Article was successfully destroyed.' }        format.json { head :no_content }      end    end      private      # Use callbacks to share common setup or constraints between actions.      def set_article        @article = Article.find(params[:id])      end        # Never trust parameters from the scary internet, only allow the white list through.      def article_params        params.require(:article).permit(:headline, :content, :location, :date)      end  end  

and here's the model

class Article    include Mongoid::Document    field :headline, type: String    field :content, type: String    field :location, type: String    field :date, type: DateTime  end  

The rails server says "Error occurred while parsing request parameters" and displays this error ActionDispatch::ParamsParser::ParseError (822: unexpected token at '{'article': {'content': [u'\u0639\u064..... so what is the issue here.

I don't know if this could be of importance, but i'm getting my data through scraping Arabic newspaper articles, i noticed that all my requests to create them through the rails server were failing and so tried out a single curl request to see what the issue could be

Rails create custom list for display

Posted: 06 Nov 2016 12:03 AM PDT

In my Rails application I have a Kid model which contains the fields allergy_name_one, allergy_name_two, allergy_name_three, etc. Any specific Kid can have one, two, or three allergies, and I want to be able to output a list of all the allergies in the whole table. However since they are all separate fields I cannot come up with a way to display them. An example of what I am aiming for is:

<tbody>     <% @allergies.each do |allergy| %>         <tr>            <td><%= allergy.kid.full_name %></td>            <td><%= allergy.name %></td>            <td><%= allergy.description</td>         </tr>      <% end %>  </tbody>  

All help and approaches are appreciated, thanks!

Criticism to Single Table Inheritance

Posted: 05 Nov 2016 10:31 PM PDT

I often apply Single Table Inheritance to model classes but I found some of my colleagues are negative toward STI. They prefer "Class Table Inheritance" or "Concrete Table Inheritance".

Please refer Martin Fowler's PoEAA Pattern Catalog. http://www.martinfowler.com/eaaCatalog/index.html

Please help me clarify the issues and disprove them.

Example Schema

At first, let's assume a Customer class as a superclass and its subclasses, PersonCustomer and CompanyCustomer.

Each method will have tables as follows.

(#)=Primary Key, (*)=not null attribute

Single Table Inheritance

Customer Table

  • (#) id
  • (*) type ('PersonCustomer' or 'CompanyCustomer')
  • (*) customer_code (unique)
  • (*) name
  • (*) primary_contact_method ('email' or 'phone')
  • email
  • phone
  • date_of_birth (not null when PersonCustomer)

Class Table Inheritance

Customer Table

  • (#) id
  • (*) type ('PersonCustomer' or 'CompanyCustomer')
  • (*) customer_code
  • (*) name
  • (*) primary_contact_method ('email' or 'phone')
  • email
  • phone

PersonCustomer Table

  • (#) id
  • (*) customer_id
  • (*) date_of_birth

CompanyCustomer Table

  • (#) id
  • (*) customer_id

Concrete Table Inheritance

PersonCustomer Table

  • (#) id
  • (*) customer_code
  • (*) name
  • (*) primary_contact_method ('email' or 'phone')
  • email
  • phone
  • (*) date_of_birth

CompanyCustomer Table

  • (#) id
  • (*) customer_code
  • (*) name
  • (*) primary_contact_method ('email' or 'phone')
  • email
  • phone

Issues

Issue1) NOT NULL constraint is not applicable to columns specific to some subclasses

Yes, this is a big disadvantage. But constraints on a data model are not always enforced by database constraints.

From pure database viewpoint, what are different between the following two constraints?

  • When 'type' is 'PersonCustomer', 'date_of_birth' cannot be null (STI)
  • When 'primary_contact_method' is 'email', 'email' cannot be null (all methods)

In the real world, conditional NOT NULL constraint is very common and they are usually guaranteed by not database constraints but application layer. I don't find any reason why we cannot apply the same to 'type' of STI.

Also, if you go with Concrete Table Inheritance, you have to know that you can no longer enforce a unique constraint across tables, like customer_code above.

Issue2) impossible to force a foreign key constraint refer to a specific subclass

Let's assume CompanyCustomer can have multiple Contacts and Contact model can refer to only CompanyCustomer model. In this case, you can enforce foreign key constraint to Contact table. But along with STI, Contact record can refer to not only CompanyCustomer but also PersonCustomer.

This is similar to the NOT NULL issue. Multiple table solution can solve this issue when the condition depends on 'type' column, but cannot when it depends on any other column.

(And most of Rails projects don't use foreign key constraint.)

Issue3) common behaviors should be implemented by modules or delegation

STI is originally a technique of Object-Relational mapping. Martin Fowler introduced STI as one of the methods to "represent an inheritance hierarchy of classes" in his PoEAA. This means behaviors have nothing to do with STI.

If sharing behaviors is really problem, it's not an STI problem. We should discuss whether to apply inheritance to model classes.

Issue4) prefer polymorphic association to STI

Polymorphic association is a technique for association. STI is a technique for inheritance. They are not comparable.

Issue5) number of columns increases as number of subclasses increases and the table gets sparse due to NULL columns

Most of RDBMS have a limitation on the maximum number of columns per table. For example, MySQL(InnoDB) table can have at most 1000 columns. If it's expected that he number of columns will hit it, STI should be avoided.

On the other hand, in most of modern databases, NULL columns don't consume storage. The table may look sparse when you view the records by "select *" query, but internal data structure is not sparse.


Any comment would be appreciated.

Thanks

Rails tutorial - exception reentered error in development when I add image upload validations

Posted: 05 Nov 2016 10:23 PM PDT

I'm nearing the end of Michael Hartl's Ruby on Rails Tutorial, and just as I come to the end of implementing image uploads to the user microposts, I'm getting a very cryptic error: exception reentered.

It seems to think the problem is in my create action under the microposts controller:

 def create      @micropost = current_user.microposts.build(micropost_params)      if @micropost.save        flash[:success] = "Micropost created!"        redirect_to root_url      else        @feed_items = []        render 'static_pages/home'      end    end  ...    private      def correct_user      @micropost = current_user.microposts.find_by(id: params[:id])      redirect_to root_url if @micropost.nil?    end    

Specifically, it highlights the if @micropost.save line.

Now, after finding little help online, I used my rudimentary paste-Michael's-code-over-my-code technique, and, amazingly, I got the error to disappear. I narrowed it down to the micropost model, this causes a problem:

class Micropost < ApplicationRecord    belongs_to :user    default_scope -> { order(created_at: :desc) }    mount_uploader :picture, PictureUploader    validates :user_id, presence: true    validates :content, presence: true, length: { maximum: 140 }    validate  :picture_size      private       # Validates the size of an uploaded picture.     def picture_size      if picture.size > 5.megabytes        errors.add(:picture, "should be less than 5MB")      end     end  end  

This does not:

class Micropost < ApplicationRecord    belongs_to :user    default_scope -> { order(created_at: :desc) }    mount_uploader :picture, PictureUploader    validates :user_id, presence: true    validates :content, presence: true, length: { maximum: 140 }    validate  :picture_size      private        # Validates the size of an uploaded picture.      def picture_size        if picture.size > 5.megabytes          errors.add(:picture, "should be less than 5MB")        end      end  end  

Is this an issue of indentation? I don't know. Anyway, so I moved on to deployment with Amazon web services and heroku, and I encountered another error, this entry in my heroku logs stuck out:

2016-11-06T04:53:11.814607+00:00 app[web.1]: F, [2016-11-06T04:53:11.814544 #10] FATAL -- : [...]     2016-11-06T04:53:11.814575+00:00 app[web.1]: ):  2016-11-06T04:53:11.814575+00:00 app[web.1]:   :status_line   => "HTTP/1.1 403 Forbidden\r\n"  2016-11-06T04:53:11.814662+00:00 app[web.1]: F, [2016-11-06T04:53:11.814615 #10] FATAL -- : [...] app/controllers/microposts_controller.rb:7:in `create'  

So I can't just wipe my hands clean and move on. This constitutes my question: -broadly speaking, what is the "exception reentered" error? -is my indentation theory plausible? -any idea how I might fix this.

Thank you so much, I'm very green, so let me know if you need more information (and be patient if this turns out to be a stupid mistake).

No comments:

Post a Comment