ruby

add a associated value in ruby on rails from independent page

柔情痞子 提交于 2021-01-29 08:51:34
问题 im beginner of ruby on rails. i have brand - products list. Brand, class Brand < ApplicationRecord has_many :products, dependent: :destroy validates :title, presence: true, length: { minimum: 2 } end Product, class Product < ApplicationRecord belongs_to :brand end products.controller class ProductsController < ApplicationController skip_before_action :verify_authenticity_token def edit @product = Product.find(params[:id]) end def new @product = Product.new end def update @brand = Brand.find

Create a valid JSON string from either Bash or Ruby?

一个人想着一个人 提交于 2021-01-29 08:46:46
问题 I would like to create 20 droplets in DigitalOcean, and would like to do it from either Bash or Ruby. Bash seemed at first to be the easiest, but then I turned out JSON is super picky about quotes and demands the -d argument to have single quotes. So my script below doesn't expand the $line variable =( Question So now I am thinking, would it at all help if I used Ruby? Wouldn't I end up in the same problem again, just in another language? token=123 while read line; do curl -qq -X POST -H

Rails how to save the requested url?

半城伤御伤魂 提交于 2021-01-29 08:35:25
问题 I have two different ways to access the url "localhost:3000/childrens/new". I have a drop down in childrens/new page and when the user selects an option through the drop down, it shows the different partials using ajax to call the childrens#new method. accessing the childrens new page from url "localhost:3000/parents" accessing the childrens new page from url "localhost:3000/parents1" After the children have been successfully created, the user should be redirected to the relevant url (either

Stripe element form is not visible in a rails app

泪湿孤枕 提交于 2021-01-29 08:31:33
问题 I am using Stripe elements and the form is not displaying in my rails app. I have tried many suggestions and still no improvement. I am using ruby 2.3 & Rails 5.2.3 but not bootstrap. charges.js document.addEventListener("turbolinks:load", function() { const public_key = document.querySelector("meta[name='stripe-public- key']").content; const stripe = Stripe(public_key); const elements = stripe.elements(); const style = { base: { color: '#32325d', } }, invalid: { color: '#fa755a', iconColor:

Google API Server-to-Server Communication not working (Ruby implementation)

混江龙づ霸主 提交于 2021-01-29 08:05:59
问题 I have followed all the steps to set up a Domain-Wide Delegation of Authority (https://developers.google.com/admin-sdk/reports/v1/guides/delegation) and followed this link too (https://developers.google.com/identity/protocols/OAuth2ServiceAccount#delegatingauthority) and I ended up creating this class in Ruby. class Gsuite::ServiceAccount def initialize(person: nil) end def authorized_client Google::Auth::ServiceAccountCredentials.make_creds( authorizer = Google::Auth:

rails http response to Donwload excel file

女生的网名这么多〃 提交于 2021-01-29 07:45:52
问题 i saw this code in github require "csv" csv_str = CSV.generate do |csv| csv << ["awesome", "csv"] end result = IO.popen("secure-spreadsheet --password secret", "r+") do |io| io.write(csv_str) io.close_write io.read end File.open("output.xlsx", "w") { |f| f.write(result) } this code store a Excel file(output.xlsx) in my project file. how can i convert this "store file scenario" in to "download the file in the browser"? 回答1: In your config/initializers/mime_types.rb register the xlsx mime_type

Insert into Rails Database

谁都会走 提交于 2021-01-29 07:44:22
问题 I'm new to Ruby on Rails and wanted to create a crawler that scrapes data and inserts it into the database. I'm currently using Heroku so I can't access the database directly and was wondering what the best way to integrate a crawler script into the RoR framework would be. I would be using an hourly or daily cron to run the script. 回答1: If you are using Rails on Heroku you can just use an ORM adapter like Datamapper or ActiveRecord. This then gives you access to your database but through a

How to make a scrollable frame with canvas in Ruby TK?

这一生的挚爱 提交于 2021-01-29 07:20:19
问题 I need to scroll a frame that contains multiple labels. Since frames aren't scrollable, so i've choosed to use a canvas that contains a frame with these labels in it. But this would be the first time for me using a canvas, so i really don't know where to start, i ended up with this code: canvas=TkCanvas.new(root) {grid :row =>0, :column =>0} frame=TkLabelframe.new(canvas) {grid :row =>0, :column =>0} scroll=Tk::Tile::Scrollbar.new(root) {orient 'vertical'; grid :row =>0, :column =>1, :sticky

Simple_form_for action hitting wrong URL on edit in rails

泪湿孤枕 提交于 2021-01-29 07:18:39
问题 I have created a simple_form_For common for both new and update, currently, it's working fine for new but for edit/update, it calling the wrong URL. class NewsfeedsController < ApplicationController before_action :find_post, only: [:show, :destroy, :edit, :update] def index @posts = Post.all.order("created_at DESC") end def show # before_action is taking care of all 4 i.e(sho,edit,update and destroy)..Keeping it DRY end def new @post = Post.new end def create @post = Post.new(post_params) if

Sort array of hashes based on two properties

浪子不回头ぞ 提交于 2021-01-29 07:17:06
问题 I have an array of hashes which I'd like to sort by one of the values. However, if two hashes have the same value if like to sort them on a second (backup) value. In the example below I'd like to sort the hashes based of the value of "a". If two elements have the same value for "a" I'd like to sort by the value of "b" as a backup. # Notice the first and last hashes have the same value for "a" hashes = [ {a: 2, b: 7}, {a: 1, b: 0}, {a: 9, b: 6}, {a: 2, b: 8} ] Output {a: 1, b: 0}, {a: 2, b: 7}