ruby-on-rails-4

Pundit policies with two input parameters

橙三吉。 提交于 2021-02-06 15:32:07
问题 I'm pretty new with Rails and I have a problem with the following policies (using Pundit): I'd like to compare two objects: @record and @foo , as you can see here: class BarPolicy < ApplicationPolicy def show? @record.foo_id == @foo end end I don't reach to find a good way to pass a second parameter to pundit methods (@foo). I'd like to do something like: class BarsController < ApplicationController def test authorize bar, @foo, :show? # Throws ArgumentError ... end end But the Pundit

Inserting checkbox array into database

谁说我不能喝 提交于 2021-02-05 07:57:05
问题 I am stuck in that although my array parameter is being captured, it fails to insert it into the database. I do not get an unpermitted parameters error or anything. It just fails to recognize the array when inserting to the DB. What I would like to do: Capture any box that is checked off, and insert the data as separate rows into the database. Here is what I have: /subscribe/categories/2 <div> <%= simple_form_for @subscription do |f| %> <div class="form-inputs"> <%= f.hidden_field :dashboard

Find records in deeply nested associations with rails

▼魔方 西西 提交于 2021-02-05 07:14:25
问题 I have the following models: class Book < ApplicationRecord has_many :chapters end class Chapter < ApplicationRecord belongs_to :book has_many :pages end class Page < ApplicationRecord belongs_to :chapter has_many :paragraphs end class Paragrpah < ApplicationRecord belongs_to :page end Now I want to get a list of all paragraphs in a specific book. Something like: @parapgraphs = Paragraph.pages.chapters.book.where(:title => 'My Book') When I do this, I get: undefined method 'chapters' for

Why does this nested content_tag not render properly?

狂风中的少年 提交于 2021-02-05 06:17:04
问题 I have this in my helper: def favorites_count(node) content_tag :span, class: "card-favorite-count" do content_tag :i, class: "icon-heart" node.cached_votes_total end end That is being called like this in the view: <%= favorites_count(node) %> And that renders this: <span class="card-favorite-count"></span> How do I get it to render the entire thing? Edit 1 Per @tolgap's suggestion below, I tried this: def favorites_count(node) content_tag :span, class: "card-favorite-count" do content_tag(:i

FATAL: bogus data in lock file “postmaster.pid” after repairing disk and changing the data folder location postgres docker

牧云@^-^@ 提交于 2021-01-29 16:18:53
问题 We had a problem with our hard drive which contains the data folder for postgres database we repaired that hard and move the data folder to new hard drive now when we try to start our db container we get following error. db_1 | db_1 | PostgreSQL Database directory appears to contain a database; Skipping initialization db_1 | db_1 | FATAL: bogus data in lock file "postmaster.pid": "" I tried to remove the postmaster.pid file but then I get the following error, db_1 | PostgreSQL Database

Ruby array to js array in .js.erb file on rails

[亡魂溺海] 提交于 2021-01-28 18:14:09
问题 I'm trying to turn a ruby array into a js array in a js.erb file. I've checked all questions on this in stack overflow but it isn't working for me. Here is the function function c(c) { js_array = <%= raw @keys.to_json %>; b.selection.remove(), b.html.insert('js_array[0]'); } It throws the error Uncaught TypeError: Cannot read property '0' of null Im still learning js so im not sure what is going wrong here The ruby @keys array defiantly has data in it because I'm printing that on the same

Rails 4 low-level caching not working

£可爱£侵袭症+ 提交于 2021-01-28 15:03:13
问题 I think is not working because I test it with a real db situation, and always returns the content of db Execute the Rails.cache.fetch Modify the database Execute again the Rails.cache.fetch , and here It not should return the new value that I've modified in db. but it happens, not caching is executed class Translation < ActiveRecord::Base def self.translate(es_text,locale=I18n.locale) Rails.cache.fetch("#{es_text}/#{locale}", expires_in: 1.month) do trad=self.find_by_es_text(es_text)

Rails 4 low-level caching not working

无人久伴 提交于 2021-01-28 14:43:54
问题 I think is not working because I test it with a real db situation, and always returns the content of db Execute the Rails.cache.fetch Modify the database Execute again the Rails.cache.fetch , and here It not should return the new value that I've modified in db. but it happens, not caching is executed class Translation < ActiveRecord::Base def self.translate(es_text,locale=I18n.locale) Rails.cache.fetch("#{es_text}/#{locale}", expires_in: 1.month) do trad=self.find_by_es_text(es_text)

Rails4 ActiveRecord has_one :conditions key removed

我与影子孤独终老i 提交于 2021-01-28 12:10:40
问题 I am converting a RoR3.2 app to v4.2. In some models we have this construct: # A correspondent can have only one currently active client role or # it may have none. has_one :active_client, :class_name => 'Client', :conditions => IsActiveRow I am getting this error: Unknown key: :conditions. Valid keys are: :class_name, :class, :foreign_key, :validate, :autosave, :dependent, :primary_key, :inverse_of, :required, :as, :foreign_type (ActionView::Template::Error) Which is raised here: /home

RSpec controller for nested resources, before_action

十年热恋 提交于 2021-01-28 07:10:26
问题 Let say I have setup like this: config/routes.rb resources :graves do resources :candles end app/models/grave.rb has_many :candles, dependent: :destroy app/models/candles.rb belongs_to :grave app/controllers/candles_controller.rb before_action :get_grave def create @candle = @grave.candles.new(candle_params) respond_to do |format| if @candle.save format.html { redirect_to @grave, notice: 'Candle was successfully created.' } format.json { } else format.html { render :new } format.json { render