Posts

Showing posts with the label ruby-on-rails

How to preserve '+' sign in params in rails 5

How to preserve '+' sign in params in rails 5 I have an uri like this /store?email=google+@gmail.com . Since '+' sign is allowed in email address, I need to preserve the plus sign. However, in rails 5, if I use params[:email] , I get google @gmail.com instead. How to prevent rails from converting '+' into space? I know we can first encode the url, and then decode it to preserve the '+' sign, but can I directly use params instead of extracting params from uri? Is there a way to preserve the plus sign in params ? /store?email=google+@gmail.com params[:email] google @gmail.com params params Also I tried using URI.decode_www_form_component params[:email] ,which still didn't work. And since the url is provided by external source I can't ask them to explicitly encode + as %2B . URI.decode_www_form_component params[:email] + %2B Any suggestions will be appreciated! How are you using that URI? Some things will URI escape,...

How to find instance of an object in a collection within a collection in rails

How to find instance of an object in a collection within a collection in rails I'm trying to write an if statement that will find an instance of an object in a collection within another collection... House has_many :occupants Occupant has_many :shirts belongs_to :house Shirt belongs_to :occupant So if I want check if any of the occupants of house own a white shirt, I want to do something like this: <% if @house.occuptants.shirts.where(:color => 'white') %> However when I do that I get an error: undefined method `shirts' for #< Occupant::ActiveRecord_Associations_CollectionProxy I believe because occupants is a collection in this case, but I'm not sure what the correct approach or syntax should be. 2 Answers 2 Something simpler and that will help you later for different use cases is to add more to the relationships: class House has_many :occupants has_many :shirts,...

Kubernetes tries to start Rails twice on the same pod

Kubernetes tries to start Rails twice on the same pod I'm trying to get my dokerized Rails app to run on Kubernetes hosted at GCP. kubectl apply -f k8s/webshop.yml kubectl get pods NAME READY STATUS RESTARTS AGE shop-lcvxc 2/3 CrashLoopBackOff 23 1h So far so good, but the app will not start. Investigating further reveals that it has tried to start Rails (Puma) twice. Any idea why this happens? Logs $ kubectl logs shop-lcvxc -c webshop => Booting Puma => Rails 5.2.0 application starting in development => Run `rails server -h` for more startup options initialize PushNotifications Puma starting in single mode... * Version 3.11.4 (ruby 2.4.4-p296), codename: Love Song * Min threads: 5, max threads: 5 * Environment: development * Listening on tcp://0.0.0.0:3001 Exiting /usr/local/bundle/gems/puma-3.11.4/lib/puma/binder.rb:270:in `initialize': Address already in use - bind(2) for "0.0.0.0" port 3001 (Errno:...

Using “ApplicationController.new.render_to_string” in a Rails api app (config.api_only = true)

Using “ApplicationController.new.render_to_string” in a Rails api app (config.api_only = true) I have a Rails 5 app build as an api app: # config/application.rb module MyApp class Application < Rails::Application config.api_only = true end end In one of my controller actions I need to render some Ruby flavoured html, and then serve it back in the json response. A standard Rails app do this: # app/controllers/my_controller.rb def my_action html = ApplicationController.new.render_to_string("my_erb_or_haml_file", locals: {foo: "bar"}, layout: false) render :json => {html: html}, :status => :ok end However, for my slimmed api app ApplicationController.new.render_to_string seems to just render " " . This confuses me, because I would expect it to either serve the content or raise an exception. ApplicationController.new.render_to_string " " Any suggestions what route I should take in order to generate Ruby flavoured html? This ques...

How can run. Rails web app in AWS Lambda server

How can run. Rails web app in AWS Lambda server AWS (Amazon web service) introduced serverless computing commonly known as AWS Lamda, We can store the asset in S3 then deploy a lambda function to handle our business logic. Lamda will support ruby script. Is there any way to run a dynamic web framework like rails or Sinatra in Lambda server? 1 Answer 1 The main problem here is that AWS Lambda does not currently support Ruby. AWS Lambda supports code written in Node.js (JavaScript), Python, Java (Java 8 compatible), and C# (.NET Core) and Go However if you really want to run a rack (including Sinatra and Rails) based app it is possible, by bundling your own version of ruby in your deployment package. This blog post goes into detail about how to do that. Whether this is a good idea probably depends on what you are trying to achieve. By clicking "Post Y...

Deploy Andorid/iOS app on free Heroku account for Stripe payments

Deploy Andorid/iOS app on free Heroku account for Stripe payments I'm creating an application for Android/iOS both and implemented Stripe for payments, so I've deployed it on Heroku for free. So my concern is that can I run Stripe on the free account (Heroku) without any issues (means we can deploy/run application by using live secret key as well or is it only for testing purpose) or do I need to use my personal paid server for the Stripe deployment to interconnect with android/iOS apps. Referring to this link: Stripe for free - Example App Backend (Deploy to Heroku) and what does this sentence mean This is intended for example purposes only: you'll likely need something more serious for your production apps. which is available on the above link. 1 Answer 1 I'm using the stripe/example-ios-backend codebase (Ruby), so after spending a lot of time by searching on Internet and also tal...

Ruby and Rails: Show login form content of one view into the another view files modal popup

Ruby and Rails: Show login form content of one view into the another view files modal popup I want to open modal on button click of the coursedetail.html.erb the file which is done using a jquery modal. But now I want to show my login.html.erb file content in this modal so that user will be able to login directly from the modal login form. coursedetail.html.erb modal Put your login in a partial and render it in the modal. – Marc Talbot Jun 29 at 10:49 1 Answer 1 You can you partial to render the view in rails. To know more about partial click here partial By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your ...

Rails ActiveRecord include non-related tables into query

Rails ActiveRecord include non-related tables into query I have model Benchmark . It has method scores which is doing some calculations. During this calculations, it uses all values from table Synonyms . It has the corresponding model, but these models ( Benchmark and Synonym ) are not related to each other. I always need all synonyms in any of my benchmark calculation. Synonyms are used to replace values in a couple of strings which are Benchmark attributes. To get the synonyms I simply use the Synonym.all method inside my scores method: Benchmark scores Synonyms Benchmark Synonym Synonym.all scores def scores @synonyms ||= Hash[Synonym.all.map {|synonym| [synonym.synonym, synonym.word]}] @scores ||= # replacing substrings in Benchmark name using synonyms end So far so good. But now I want to perform bulk calculations for all the benchmarks I have. This obviously leads to N+1 query problem — each scores method calls Synonym.all method. Rails is smart enough to cache...

NameError in AttachmentsController#indicateur

NameError in AttachmentsController#indicateur I'm on Ruby on rails and I started with the library axlsx_rails. By cons when I get my data from my table to put it in an xlsx file it gives me this error undefined local variable or method` attachment 'for # < AttachmentsController: 0x007f7d8ecf1a08> Did you mean? Attachment_url I attach the files of my program if someone has any idea. Thank you. route.rb view: indicateur.xlsx.axlsx controller view: indicateur.html.erb Next time please post your code, do not post an image. – araratan Jun 29 at 9:53 1 Answer 1 It is giving you that error because it is looking for attachment method. I believe you want to do is to fecth all the attachments. So instead of you code: attachment @attachments = atta...

DataTable search not working for dynamic rows

Image
DataTable search not working for dynamic rows I have following table #physician-privellages on which I have implemented DataTable . #physician-privellages DataTable .table table.table.table-striped#physician-privellages thead tr th First Name th Last Name th Email th Patient Privileges th Visit Privileges - if Authorizer.physician_practice? th My Practice th Actions tbody.privileged-physician-records - user_permissions_for_physicians = physician_privileges_permissions(user_id) - user_permissions_for_physician = user_permissions_for_physicians[user_id] - user_permissions_for_physician&.delete(user_id) - user_permissions_for_physician&.each do |user_id, user_permissions| tr td = "#{user_permissions[:physician].first_name}" td = "#{user_permissions[:physician].last_name}" td = "...

Finding posts under a category name keyword with pg_search on Rails 5

Finding posts under a category name keyword with pg_search on Rails 5 I'm using gem pg_search and doing multi-search for Website and Post tables. I am able to search by records title, but I also want to be able to search records based on categories they belong to. pg_search So, for example, when I type "typographic" (category name) to search input, all Website and Post records under that category should be listed, as well as all records that have "Typographic" in the title. I think it can be done with association but I could not make it work 🤷‍♂️ _header.html.erb <%= form_tag search_index_path, method: :get do %> <%= text_field_tag :query, params[:query], placeholder: "Search" %> <% end %> search_controller.rb class SearchController < ApplicationController def index @pg_search_result = PgSearch.multisearch(params[:query]) end end models/post.rb class Post < ApplicationRecord include PgSearch multisearchable :ag...

Add template with inline styles to ckeditor

Image
Add template with inline styles to ckeditor I'm trying to add this html to ckeditor template Firstly, in assets/javascripts/ckeditor/config.js with config.allowedContent = true; I allowed style tags And then, In assets/javascripts/ckeditor/plugins/templates/templates/default.js I added the following code <style> .body {color:#474d5d; font-size: 13px;} h1 {font-size:18px; font-weight: 400;text-align:center} h2 {color:#233974; font-weight:700; font-size:14px;border-bottom:2px solid #233974;} h2 img {width:15px; margin-right: 5px;} li {margin-bottom: 10px;} ul.exp, ul.formacion{margin-bottom: 40px;} .company{border:0px solid black; float:right; margin-bottom:0px; margin-left:0px; margin-right:0px; margin-top:0px; width:150px} .img-formacion {width: 20px;} .position{text-align:center; font-size:18px; margin-bottom: 40px;} .CV {clear:both; text-align:center; font-size:18px;} .n_1{margin-top: 10px;} </style> Curriculum Vitae [Name] [Position] EXPERIENCIA PROFESIONAL [ desde...

I want to hide the packages included in drop down list without touching my model

I want to hide the packages included in drop down list without touching my model I have a drop-down list which has to choose the packages. But currently, I want to hide some packages for now without touching my product model. Please help me to solve this problem. Thanks! <div class="col-sm-8 col-md-9"> <%= p.select :product_id, options_for_select( Listing::Product::PACKAGES.map { |k,v| [ v[:name], v[:rank] ] }, selected: p.object.product_id), {include_blank: true}, class: "form-control" %> <small> <%= p.label I18n.t('admin.listings.edit.configuration.select_product') %> </small> 2 Answers 2 In the PACKAGES constant kindly remove the not required items. Then write a separate method call from before_filter . Inside that method add your additional items to the PACKAGES based on your condition(s). PACKAG...

fallback method not working in i18n-js with rails 2.3

fallback method not working in i18n-js with rails 2.3 I am integrating I18n-js in a Rails application. The integration is working fine along with the translations.But the problem I am facing is that when the required translation is not present in a particular language , say for example I selected Afrikaans as my language then in that case the fallback method should execute showing me the translation present in the default language.But its not happening and I get the error [missing "af.credential_requests.add" translation] This surely means that the fallbacks method is not working. Though I have I18n.fallbacks = true; in my I18n-js. Please help. I have my default locale set to "en" 2 Answers 2 Make sure that you set I18n.defaultLocale correctly I18n.defaultLocale The I18n.defaultLocale is set correctly. – Anirudh Rautela ...

How do I customize the banner/header image of Hyrax/Samvera?

How do I customize the banner/header image of Hyrax/Samvera? I'm a web developer, trying to get my bearings with the Samvera-based application known as Hyrax. I'm using version 2.1.0.rc3. I have done some devops-type stuff with Hyrax so far, I have an app running on AWS and can deploy new code to that stack via Capistrano... but I'm tired of looking at the default header image... time to move in... however, I'm stuck. I've tried searching for terms like image-masthead in the application code, but so far, no luck. I have a suspicion this is a frequently asked question, so I'd accept as an answer just a link to a howto. But... in my searching for an answer I have not yet found such a document. So, I'm turning to Stack Overflow. Help? @sticky-bit what was wrong with the edit? Hardy is my colleague; I was just helping make their first post more specific, since it's about the banner image. And so our community members can find this...

ArgumentError: wrong number of arguments (given 0, expected 2..3)

ArgumentError: wrong number of arguments (given 0, expected 2..3) I'm trying to get searchkick to work but I run into an error when I attempt to reindex my stories via the console. I can't seem to understand what the error might be and would love some help. The error via the console after entering "Story.reindex": Story Import (3.6ms) {"count":2,"exception":["ArgumentError","wrong number of arguments (given 0, expected 2..3)"],"exception_object":"wrong number of arguments (given 0, expected 2..3)"} ArgumentError: wrong number of arguments (given 0, expected 2..3) from app/models/story.rb:42:in `hot_score' from (irb):1 story.rb model: class Story < ApplicationRecord searchkick has_many :votes scope :newest, -> { order(created_at: :desc) } scope :hottest, -> { order(hot_score: :desc) } def upvotes votes.sum(:upvote) end def downvotes votes.sum(:downvote) end def calc_hot_score points =...