ruby-on-rails-5

Including a module in a Rails 5 Application Record class

廉价感情. 提交于 2019-12-12 11:29:28
问题 I am using Application Record to simplify shared logic throughout an application. Here's an example that writes a scope for a boolean and its inverse. This works well: class ApplicationRecord < ActiveRecord::Base self.abstract_class = true def self.boolean_scope(attr, opposite = nil) scope(attr, -> { where("#{attr}": true) }) scope(opposite, -> { where("#{attr}": false) }) if opposite.present? end end class User < ApplicationRecord boolean_scope :verified, :unverified end class Message <

Using ActionCable with multiple identification methods

一曲冷凌霜 提交于 2019-12-12 10:54:25
问题 I develop a Ruby on Rails 5.1 application using ActionCable. User authentification via Devise works fine for several channels. Now, I want to add a second type of channels which does not require any user authentification. More precisely, I would like to enable anonymous website visitors to chat with support staff. My current implementation of ApplicationCable::Connection for authenticated users looks like this: # app/channels/application_cable/connection.rb module ApplicationCable class

Pass rails method(association) to React Js

南笙酒味 提交于 2019-12-12 06:09:20
问题 In My React js application I need to pass data of my Product Model's association model(say "Taxon" ),i am doing it successfully using Active record serializer as ActiveModelSerializers::SerializableResource.new(object, options).serializable_hash where i pass options as all association relation. i can get whole array of taxon objects as json,But I want something mentioned As following i using rails as backed i am calling method like Prodct.first.category.name which gives => "mugs" the method

Rails - Unknown attribute password

空扰寡人 提交于 2019-12-12 05:58:11
问题 I have been following Michael Hartl's Ruby on Rails tutorial book to try and add users to my application. Reading chapter 6, I have added what I believe to be the necessary fields for my user, specifically password and password confirmation via "has_secure_password". I thought that adding "has_secure_password" to my user model would include the attributes "password" and "password_confirmation" provided I add a "password_digest" to the model. I have done that as the book instructed me to.

Is there a validation I can use for a specific attribute on all associated records?

假如想象 提交于 2019-12-12 05:38:07
问题 I have a Question that has_many :answers (just like SO). I also want each question to have only 1 accepted_answer , so I just added an :accepted attribute to the Answer model that is simply a boolean. So now, to get the accepted answer for my question, I have written a method on my model that just does this: def accepted_answer answers.where(accepted: true) end That allows me to do question.accepted_answer and it returns an ActiveRelation object like you would expect. Nothing fancy. Simple

Rails nested attributes not saving using carrierwave

浪子不回头ぞ 提交于 2019-12-12 04:45:31
问题 I can get the gallery attributes to insert as evident by server log below but picture attributes will not insert as well. Server response Started POST "/galleries" for 127.0.0.1 at 2017-05-13 18:19:23 +1000 Processing by GalleriesController#create as HTML Parameters: {"utf8"=>"✓", "authenticity_token"=>"LACaMz44B9mn/psLYjzs8qrwo9mr0l2OEIPg+VmCn9CdbGhBh9rDUJ6FE0EOwKCj7aZVjbM4+t0YoaFIRX7IEA==", "gallery"=>{"name"=>"Hello", "cover"=>"123456", "picture"=>{"picture"=>#<ActionDispatch::Http:

Rails 5: Not converting DateTime object timezone correctly

那年仲夏 提交于 2019-12-12 04:38:30
问题 This issue has been eating away at me for a while. I take a date time string: [14] pry(#<EventsController>)> params[:event][:ended_at] => "05/31/2017 2:00 PM" I convert it to a DateTime object: pry(#<EventsController>)> to_datetime = DateTime.strptime(params[:event][:ended_at], "%m/%d/%Y %H:%M %p") => Wed, 31 May 2017 14:00:00 +0000 If I run the in_time_zone method on the to_datetime object it outputs the wrong time for pacific timezone: [16] pry(#<EventsController>)> to_datetime.in_time_zone

Add methods to a Rails engine model from a Rails plugin

瘦欲@ 提交于 2019-12-12 04:38:16
问题 I'm writing a Rails plugin to extend a Rails engine. Namely MyPlugin has MyEngine as a dependency. On my Rails engine I have a MyEngine::Foo model. I'd like to add new methods to this model so I created a file in my plugin app/models/my_engine/foo.rb which has the following code: module MyEngine class Foo def sayhi puts "hi" end end end If I enter the Rails console on the plugin dummy application I can find MyEngine::Foo , but runnning MyEngine::Foo.new.sayhi returns NoMethodError: undefined

Navigation-based redirections after sign up

百般思念 提交于 2019-12-12 04:28:36
问题 My website allows users to browse freely up to the point where they need to be signed in to continue using the service (hitting the "sign up" button and going through the registration process). Once the users sign up, I want to redirect them to the last interesting page they were browsing : it might be the page just before clicking the sign-up button, or something more complex. For instance suppose the user browses a "core" content page, then goes to the "about_us" page, then tries to sign up

Optional many to many relationship in rails?

和自甴很熟 提交于 2019-12-12 04:28:12
问题 Is there a way to produce an optional many to many relationship in rails? I have a Client model and a ClientManager model - And I want a Client to have 0, 1 or many client_managers . But I'd also like a Clientmanger to be able to have 0, 1 or more clients . Is this possible? And how? Using rails 5. Updated for clarity/purpose: I want clientmanagers, to be able to manage multiple clients, or even none. And clients to be able to be managed by 0, 1 or multiple client-managers 回答1: If it's an NxN