activemodel

How to validate in a model, data from a controller

亡梦爱人 提交于 2019-12-01 18:25:14
So I have some data that gets pulled from another rails app in a controller lets call it ExampleController and I want to validate it as being there in my model before allowing the wizard to move to its next step and I can't quite figure out how I should be doing it ( I know that getting this data directly from the controller into the model violates MVC I am looking for the best workaround to get my data from the controller ) . The data must come from the controller as the methods for getting it are contained in ApplicationController however I could do this in the Awizard controller if this is

How to validate in a model, data from a controller

末鹿安然 提交于 2019-12-01 17:14:54
问题 So I have some data that gets pulled from another rails app in a controller lets call it ExampleController and I want to validate it as being there in my model before allowing the wizard to move to its next step and I can't quite figure out how I should be doing it ( I know that getting this data directly from the controller into the model violates MVC I am looking for the best workaround to get my data from the controller ) . The data must come from the controller as the methods for getting

NoMethodError (undefined method `empty?' for #<Event:0x6042e88>):

廉价感情. 提交于 2019-12-01 10:35:18
I created a rails API using postgre database in which i have a model (table) name as counseling_event NOTE: Not using scaffold and using rails-5 My scheman(migration) for counseling_event is as follow: class CreateCounselingEvent < ActiveRecord::Migration[5.0] def change create_table :counseling_event do |t| t.text :name t.datetime :start_time t.datetime :end_time t.text :location t.integer :user_id t.integer :role_id t.timestamps end end end I added a column to the above table, which is of type enum. column name is event_type Now, In my Controller i.e CounselingEventController, in my action

NoMethodError (undefined method `empty?' for #<Event:0x6042e88>):

社会主义新天地 提交于 2019-12-01 08:28:10
问题 I created a rails API using postgre database in which i have a model (table) name as counseling_event NOTE: Not using scaffold and using rails-5 My scheman(migration) for counseling_event is as follow: class CreateCounselingEvent < ActiveRecord::Migration[5.0] def change create_table :counseling_event do |t| t.text :name t.datetime :start_time t.datetime :end_time t.text :location t.integer :user_id t.integer :role_id t.timestamps end end end I added a column to the above table, which is of

Ruby on Rails 3 (3.1) ActiveModel Associations (tableless nested models)

非 Y 不嫁゛ 提交于 2019-12-01 04:10:17
How to impliment ActiveModel associations (tableless nested models)? For example: book has many chapters With ActiveRecord I would create two models and assosiate them with has_many and belongs_to . But ActiveModel doesn't have such functionality. How can I implement this? You simply can't do it that way. It is not active record. You can check ActiveModel documentation (and source code) at : https://github.com/rails/rails/tree/master/activemodel I guess you have to do it old fashion way, using an array of chapters and a reference to the book in the chapters. Hope this helps! With rails

Rails validatation to ensure a username does not clash with an existing route?

纵饮孤独 提交于 2019-12-01 01:27:49
问题 I want to ensure users can't create usernames that clash with my existing routes. I would also like the ability to deny future routes I may define. I am thinking of accomplishing this like so: In the model: class User < ActiveRecord::Base @@invalid_usernames = %w() cattr_accessor :invalid_usernames validates :username, :exclusion { :in => @@invalid_usernames } end In some initializer: User.invalid_usernames += Rails.application.routes.routes.map(&:path).join("\n").scan(/\s\/(\w+)/).flatten

Ruby on Rails 3 (3.1) ActiveModel Associations (tableless nested models)

好久不见. 提交于 2019-12-01 00:46:36
问题 How to impliment ActiveModel associations (tableless nested models)? For example: book has many chapters With ActiveRecord I would create two models and assosiate them with has_many and belongs_to . But ActiveModel doesn't have such functionality. How can I implement this? 回答1: You simply can't do it that way. It is not active record. You can check ActiveModel documentation (and source code) at : https://github.com/rails/rails/tree/master/activemodel I guess you have to do it old fashion way,

Where are Default Validation Error Messages in Rails 3.0?

拈花ヽ惹草 提交于 2019-11-30 17:30:41
Where are the default validation error messages in Rails 3.0? What is the equivalent of ActiveRecord::Error.default_error_messages[:taken], for example? I have gotten as far as finding that ActiveModel handles the errors rather than ActiveRecord, but I can't find the errors themselves. http://github.com/rails/rails/blob/master/activemodel/lib/active_model/locale/en.yml and http://github.com/rails/rails/blob/master/activerecord/lib/active_record/locale/en.yml :D UPDATE: Maybe you should try to add your own custom error messages? # de.yml activerecord: errors: messages: taken: "ist bereits

What is the ActiveModel method attribute “_was” used for?

拥有回忆 提交于 2019-11-30 08:54:47
When using autocomplete in the console, I often see " _was " postpended to my attributes. But I can't find any documentation or best practices for usage. What does it do and how should it be used? Example: user.fname has the method user.fname_was Using source_location, I've tracked it down to: active_model/attribute_methods.rb", line 296 but there isn't anything specific. That is a part of ActiveModel::Dirty You can see it here https://github.com/rails/rails/blob/af64ac4e5ce8406137d5520fa88e8f652ab703e9/activemodel/lib/active_model/dirty.rb#L146 Example person = Person.find_by_name('Uncle Bob'

form_for non-AR model - fields_for Array attribute doesn't iterate

我与影子孤独终老i 提交于 2019-11-30 07:27:11
I'm having trouble getting fields_for to work on an Array attribute of a non-ActiveRecord model. Distilled down, I have to following: models/parent.rb class Parent extend ActiveModel::Naming include ActiveModel::Conversion include ActiveModel::Validations extend ActiveModel::Translation attr_accessor :bars end controllers/parent_controller.rb def new_parent @parent = Parent.new @parent.bars = ["hello", "world"] render 'new_parent' end views/new_parent.html.haml = form_for @parent, :url => new_parent_path do |f| = f.fields_for :bars, @parent.bars do |r| = r.object.inspect With the code as above