ruby-on-rails-5

Check if false then run code

*爱你&永不变心* 提交于 2019-12-12 01:24:35
问题 I want to test something. If false do this. I wish not to test for true, only false. My code looks like this and I only need to do something if false: if Model.exists?(table_column: 'joe') # this part is blank else # run this code end My code runs after the else which is what I want. Is there a clean rails/ruby method that tests for false? My code would only run if false, not true. If duplicate, I'll gladly delete. Thanks 回答1: Use unless unless Model.exists?(table_column: 'joe') # run code

Devise methods undefined in Rails project

爱⌒轻易说出口 提交于 2019-12-12 00:49:18
问题 I have a Rails 5 app with the Devise gem installed. My project is set up in such a way, that all controllers are namespaced...Even Devise controllers. In my routes file I have done this(This will better explain my namespacing structure): controllers: { sessions: "api/v1/public/members/users/sessions", passwords: "api/v1/public/members/users/passwords", registrations: "api/v1/public/members/users/registrations", }, path_names: { sign_in: 'login', password: 'forgot', sign_up: 'register', sign

How do you create parent objects for a factory using before(:create) without sparking the parents' after_create callback?

依然范特西╮ 提交于 2019-12-11 18:43:23
问题 I am trying to get this spec to pass: it "queues up AbsenteeReservationEmailerJob" do ActiveJob::Base.queue_adapter = :test expect { subject }.to enqueue_job(AbsenteeReservationEmailerJob) end My factory looks like so: FactoryBot.define do factory :absentee_reservation do amount_cents { 10_000 + rand(1_000_000) } creator { create(:user) } before(:create) do |absentee_reservation| auction = create(:auction_with_one_item_one_bidder) item = auction.items.first absentee_reservation.auction =

How can I properly install bootsnap on windows

不问归期 提交于 2019-12-11 18:05:33
问题 I'm trying to install ruby on rails with rails installer. When I use "rails new blog" to get my feet wet on starting the web page, I keep getting a build error to build a native extension. I've also have similar problems installing sqlite3. The same issues appear on my laptop as well. (Both windows) So far I tried downloading this walkthrough (https://github.com/Shopify/bootsnap/issues/134), but get stuck when it tells me to do the script. I tried running the given script in irb as it said to

How to ensure the javascripts/channels/chatrooms.coffee is loading and receive: (data) works? Console.log data is not loading

倖福魔咒の 提交于 2019-12-11 17:38:42
问题 Here is the file, containing what should receive from the Message Relay Job that successfully loads on the server. App.chatrooms = App.cable.subscriptions.create "ChatroomsChannel", connected: -> # Called when the subscription is ready for use on the server disconnected: -> # Called when the subscription has been terminated by the server received: (data) -> console.log(data) active_chatroom = $("[data-behavior='messages'][data-chatroom-id='#{data.chatroom_id}']").append("<div><strong>#{data

Rails 5 pdf preview with Shrine Gem

雨燕双飞 提交于 2019-12-11 17:37:43
问题 I am using Shrine Gem for image uploads and was wondering how, if possible, can I show a 1st page preview in my template like showing an image per-say. I am ok with using jQuery or other library. Below is my code for the file upload, including my Shrine initializer and uploader file. view ... <div class="col-md-4 upload-block"> <%= f.label :spec_sheet, 'Spec Sheet' %> <% if @product.spec_sheet.present? %> <div class="product-image"> <%= image_tag(@product.spec_sheet_url(:thumb)) %> <div class

Efficiently iterate and mutate an association

丶灬走出姿态 提交于 2019-12-11 17:24:32
问题 We have code that is effectively doing this. obj.things.each |thing| ... do some stuff ... obj.things.destroy(thing) ... do some more stuff... end We found out that simultaneously iterating over a CollectionProxy while changing the CollectionProxy results in only half the items being iterated over. Currently we're working around this by flattening the proxy into an array. But this means copying all the things into memory. obj.things.to_a.each |thing| ... end Is there a way to iterate and

Paperclip don't save on database

那年仲夏 提交于 2019-12-11 17:19:45
问题 I am using the following Gems: 'paperclip' 'aws-sdk', '~> 2.3' I would like to save images to S3, but am unable to get them to save. model/user.rb class User < ApplicationRecord # This method associates the attribute ":avatar" with a file attachment has_attached_file :avatar, styles: { thumb: '100x100>', square: '200x200#', medium: '300x300>' } # Validate the attached image is image/jpg, image/png, etc validates_attachment_content_type :avatar, :content_type => /\Aimage\/.*\Z/ end migration

Rails 5 app won't load JS from assets/javascripts folder

三世轮回 提交于 2019-12-11 17:14:20
问题 As I understand it any js file in the assets/javascripts folder is loaded if /= require_tree is present in the application.js file. I can't get my JS code to work, however. I have put it in assets/javascripts/locations.js and it doesn't work. When I put it in a script tag in my application.html.erb file it worked perfectly on the other hand. Any ideas what I could be missing? Running Rails 5.2. The JS code is below. locations.js var locations = ["coordinatesStore"] .map(id => document

Rails - Editing User and Profile Models from separate Settings Controller

浪尽此生 提交于 2019-12-11 17:04:44
问题 I'm struggling to figure out exactly how to get this to work, but essentially I have 2 models: class User class Profile One User has One Profile. I also have a SettingsController with "profile" and "update" actions: class SettingsController < ApplicationController def profile @profile = User.find_by_id(current_user).profile end def update set_profile respond_to do |format| if @profile.update(profile_params) format.html { redirect_to @profile, notice: 'Profile was successfully updated.' } else