Home > ruby on rails > form validation with nested models in Rails

form validation with nested models in Rails

January 22Hits:1
Advertisement

I have this problem. I need to validate the attributes of two models in the same form in Rails. One is the parent of the other.

The form is like this:

<%= semantic_form_for @professional do |pro| %>        <%= pro.inputs :id => "information" do %>           <%= pro.input :name, label: t("Artistic Name") %>          <%= pro.semantic_fields_for @user do |user| %>           <%= user.inputs :id => "register" do %>              <%= user.input :email, :placeholder=>"[email protected]" %>             <%= user.input :password, label: t('Password') %>            <%end%>         <% end %>     <% end %>  <% end %> 

The models I am using are like this:

User:

class User < ActiveRecord::Base   belongs_to :role, polymorphic: true   validates  :email, :password, presence: true  end 

Professionals:

class Professional < ActiveRecord::Base  has_one :user, as: :role, dependent: :destroy   accepts_nested_attributes_for :user     validates  :date_birthday, :gender, :height, :name, :description, :Weight, :address, :languages,:services, :category, :phonenumber, :fullname, :hair_color, :age, :orientation, presence: true  end 

So, what is the problem?

When I clicked in the submit button the professional attributes are marked but not the users attributes.

Like this:

form validation with nested models in Rails

The fields marked in red belongs to the professional model but the fields email and password belongs to the user model aren't marked in red when it should be because they are empty.

What can i do? I need the warning message for the user is attributes too

Thanks in advances.

Answers

We've achieved what you need before.

We had to use inverse_of so that the object was a singular piece of data (rather than multiple pieces as is the case by default):

#app/models/user.rb
class User < ActiveRecord::Base
  belongs_to :role, polymorphic: true, inverse_of: :user
  validates  :email, :password, presence: true
end

#app/models/professional.rb
class Professional < ActiveRecord::Base
  has_one :user, as: :role, dependent: :destroy, inverse_of: :role
  accepts_nested_attributes_for :user
end

This will help.

You also need to make sure you're passing these objects correctly (I see so many people not doing this).

You need to tell Professional to validate the associated User:

class Professional < ActiveRecord::Base
    ...
    validates_associated :user

Related Articles

  • form validation with nested models in Rails

    form validation with nested models in RailsJanuary 22

    I have this problem. I need to validate the attributes of two models in the same form in Rails. One is the parent of the other. The form is like this: <%= semantic_form_for @professional do |pro| %> <%= pro.inputs :id => "information"

  • Before validation on nested modelJanuary 9

    I have a nested model items and I am trying to multiply two columns together cost and quantity to set the last column price. I need to set the column price before the form is saved and i need to also validate the model. The before_validation call bac

  • Creating nested models in Rails 4 forum appJanuary 31

    Hello I am making a Forum application in Rails 4. It can have numerous forums, each with numerous topics. Each topic can have many posts. When creating a new topic, one must also create the initial post, much like Stack Overflow itself. Therefore, I

  • Rails - Why does my nested model form not require 'accepts_nested_attributes_for'February 9

    I have two models item and user_item. Item has many user_items and user_items belongs to item. I have a form where a user can create a new item. In the form a user should include a picture. The name, description, and tags get saved to a new item obje

  • Problem validating parent model with nested models in modelbuilder

    Problem validating parent model with nested models in modelbuilderMarch 24

    I have a parent model with several nested models. I'm having trouble getting the parent model to validate. The issue is with sub model 4. The two screenshots are of the parent model and sub model 4. The output from sub model 3 feeds into parameter "M

  • Rails routes for self nested modelsJanuary 15

    In my rails app I have a Task model. In my app, my tasks can have substasks and so on. Since my subtasks are tasks with only a parent task, I did a self-nested model such as class Task < ActiveRecord::Base ## Self Join has_many :subtasks, class_name:

  • rails with most nested models and cocoon gem;

    rails with most nested models and cocoon gem;February 16

    I have a rails4 app with nested model form. There is a product model that has_many product_features, has_many product_usecases and has_many product_competitors. Everything works fine except when I load the page only the first nested model (product_fe

  • Rails nested models and virtual attribute initializationJanuary 27

    I have a problem understanding how are attributes "sent" to nested model(s), and if is possible to do this for model with virtual attrubute too. I have three models: class User < ActiveRecord::Base ... has_and_belongs_to_many :clearancegoods

  • Complicated Validations with Many Fields in RailsFebruary 17

    I have a model (ProjectedSquad) that belongs to another model (SoldierBio) with many different foreign keys. I want to validate that certain fields have a uid value that only appears once in the row, while other fields do not need to have a unique va

  • Nested Comments with Rails

    Nested Comments with RailsMay 1

    Comments are everywhere. Blogs, social networks, fan sites, learning resources – they all have some kind of a commenting system. Often we would like to present our users options to both leave a comment and reply as well. The most natural way to repre

  • Dynamic nested form in RailsFebruary 8

    I have a Rails 3.2.22 app that I'm maintaining and I need to create a quasi-complex form. It's a form that records medications into a report. So my thought is to build a model called NarcoticUsage to contain the record then an associated/nested Model

  • Nested Iteration with model builder - using nested models

    Nested Iteration with model builder - using nested modelsSeptember 7

    I'm new to using model builder and have to do a nested iteration (for every month I have to iterate through 6 parameters). I am thinking of doing that by using nested models. One model doing the interation through monthes and then providing the curre

  • PHP MVC class with controller and nested model

    PHP MVC class with controller and nested modelNovember 26

    I had asked a first question about this class a while ago and got a few answers here which made me rewrite it completely. I removed all statics and globals, added my variables as arguments for the constructor, and pass them again as reference through

  • Using global parameters in nested models of ModelBuilder?October 22

    I have a model that consists of two nested models and various parameters. There are two parameters that are common to all of the models (including the overall "grand" model). The common parameters are in-line variables and serve to assign unique

  • Creating Instance of Nested model inside parent controllerJanuary 18

    I would like to know if it is possible to create an instance of a nested model inside the parents controller. Let me explain. I have a model called Museum and another model called Museumimage. Museumimage has only one field which is an attachment, ma

  • How to get multiple records for same model with Rails form_forJanuary 20

    I am trying to build a basic recipes app but am having trouble allowing the user to input multiple ingredients for one recipe. The array of permitted params for the ingredients ends up empty. So I guess my question is - how do I permit the array of i

  • Knockout JS Mapping fromJS nested modelsJanuary 27

    I am having trouble understanding how to work with Knockout JS Mapping Plugin. I have some nested models (as seen below) and what I am currently doing is just using the ko.mapping.fromJS() in the parent model. What I am noticing is that computed valu

  • Trying to update nested model first on before_saveFebruary 11

    I am calculating :price in my nested item model in order to map it in my invoice model. The problem is that the before_save in the invoice model is being called first and I want it to be called after the before_save in the nesteditem model Any ideas

  • Rails: nested models disappearing after validationJanuary 26

    I have a model: class Invoice < ActiveRecord::Base belongs_to :case has_many :invoice_positions accepts_nested_attributes_for :invoice_positions, allow_destroy: true And i have a form createn with https://github.com/nathanvda/cocoon but i have one sm

  • Rails - Validating complex nested formFebruary 10

    I have a form with four input fields where a user creates an item. In the form one of the four input fields is for picture which is used to create a user_item at the same time on form submission. I am running into several problems. When validation fa

Copyright (C) 2017 ceus-now.com, All Rights Reserved. webmaster#ceus-now.com 14 q. 0.451 s.