activemodel

ActiveModel::ForbiddenAttributesError + cancan + rails 4 + model with scoped controller

蓝咒 提交于 2019-12-06 07:30:29
问题 I m using cancan(1.6.10) with rails 4.0.0. I have a model called 'App'(not scoped) and a controller Admin::AppsController(its scoped. ie app/controllers/admin/apps_controller). the controller code is as class Admin::AppsController < ApplicationController before_filter :authenticate_user! load_and_authorize_resource class: App def index end #CRUD methods and some other custom methods ... private def app_params params.require(:app).permit(:name, :description, :author, :url_path, :validated,

Rails has_many :through association

▼魔方 西西 提交于 2019-12-06 04:17:14
问题 I'm trying to create a rails app where user can create events and invite participants to it and need your help! I've been going in circle, trying few things but doesn't seem right at all and this is now driving me crazy!! I'm using rails 4. How would you setup the active model? User has_many :events through :meeting //for the participants? has_many :events // for the organizer? Event belongs to :user has_many :participants, class_name: "User" Participant belongs to :user has_many :events

Rails 3: Display validation errors for a form (not saving an ActiveRecord model)

為{幸葍}努か 提交于 2019-12-05 10:59:35
Apologies if this is a really common and/or ridiculous question; I swear I've read over the documentation multiple times and everything seems so focused on ActiveRecord to the point they've wandered off the path of forms that do something other than create or edit model data. Take for example a form with inputs to control the extraction and display of some statistics. What does rails provide me with for validating the user input of this form, which won't be invoking save on any records? Things like: :email must be an email address :num_products must be a positive whole number :gender must be

Shared scopes via module?

蹲街弑〆低调 提交于 2019-12-05 09:32:27
问题 I want to dry up several models by moving shared scopes into a module, something like: module CommonScopes extend ActiveSupport::Concern module ClassMethods scope :ordered_for_display, order("#{self.to_s.tableize}.rank asc") end end I also want to create shared specs that test the module. Unfortunately when I try to include the shared scope in my model I get: undefined method `order' for CommonScopes::ClassMethods:Module Any ideas? Thanks! 回答1: You can use instance_eval module CommonScopes

Rails 3 ActiveModel: cannot include ActiveModel::Model directly

喜夏-厌秋 提交于 2019-12-05 04:33:41
In my Rails 3.2.11 and "development" environment when I try to have an active model: class DisponibilityApi include ActiveModel::Model attr_accessor :start_time, :end_time validates :start_time, :end_time, :presence => true end I have an error: NameError: uninitialized constant ActiveModel::Model But when I include it manually: class DisponibilityApi extend ActiveModel::Naming extend ActiveModel::Translation include ActiveModel::Validations include ActiveModel::Conversion attr_accessor :start_time, :end_time validates :start_time, :end_time, :presence => true end Now it works ! Am I missing

How to access params in the callback of a Rails model?

纵饮孤独 提交于 2019-12-04 19:02:50
问题 I have a callback of a model that needs to create a dependent object based on another field entered in the form. But params is undefined in the callback method. Is there another way to access it? What's the proper way to pass a callback method parameters from a form? class User < ActiveRecord::Base attr_accessible :name has_many :enrollments after_create :create_enrollment_log private def create_enrollment_log enrollments.create!(status: 'signed up', started: params[:sign_up_date]) end end

ActiveModel::ForbiddenAttributesError + cancan + rails 4 + model with scoped controller

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-04 13:44:26
I m using cancan(1.6.10) with rails 4.0.0. I have a model called 'App'(not scoped) and a controller Admin::AppsController(its scoped. ie app/controllers/admin/apps_controller). the controller code is as class Admin::AppsController < ApplicationController before_filter :authenticate_user! load_and_authorize_resource class: App def index end #CRUD methods and some other custom methods ... private def app_params params.require(:app).permit(:name, :description, :author, :url_path, :validated, :active, :version) end end I m getting error when i try to create a 'app'. ActiveModel:

Rails has_many :through association

喜你入骨 提交于 2019-12-04 10:01:39
I'm trying to create a rails app where user can create events and invite participants to it and need your help! I've been going in circle, trying few things but doesn't seem right at all and this is now driving me crazy!! I'm using rails 4. How would you setup the active model? User has_many :events through :meeting //for the participants? has_many :events // for the organizer? Event belongs to :user has_many :participants, class_name: "User" Participant belongs to :user has_many :events through :meeting Meeting has_many :participants has_many :events Does that make any sense? Do I need the

What's the correct way to make before_validation, etc. work in an ActiveModel

我与影子孤独终老i 提交于 2019-12-04 00:07:30
问题 Should I extend or include ActiveModel:Validations:Callbacks:ClassMethods or ActiveModel:Validations:Callbacks ? 回答1: I got it to work like this: class Foo extend ActiveModel::Callbacks include ActiveModel::Validations include ActiveModel::Validations::Callbacks before_validation :bar def bar # callback logic here end end It's important that you have everything in that order. 来源: https://stackoverflow.com/questions/8936906/whats-the-correct-way-to-make-before-validation-etc-work-in-an

Shared scopes via module?

南笙酒味 提交于 2019-12-03 22:20:42
I want to dry up several models by moving shared scopes into a module, something like: module CommonScopes extend ActiveSupport::Concern module ClassMethods scope :ordered_for_display, order("#{self.to_s.tableize}.rank asc") end end I also want to create shared specs that test the module. Unfortunately when I try to include the shared scope in my model I get: undefined method `order' for CommonScopes::ClassMethods:Module Any ideas? Thanks! You can use instance_eval module CommonScopes extend ActiveSupport::Concern def self.included(klass) klass.instance_eval do scope :ordered_for_display,