ruby on rails - Why Aren't These Errors Rendering For Nested Associations? -
i have mediaitem model has 2 has_one
associations. validate 1 or other nowadays using:
validates_presence_of :photo, allow_nil: true validates_presence_of :video, allow_nil: true validate :photo_or_video_present def photo_or_video_present if !(photo.blank? ^ video.blank?) errors['photo_attributes.image'] << 'select photo or video' errors['video_attributes.uid'] << 'select photo or video' end end
which gives me next error object when form rendered:
<activemodel::errors:0x007f99c8a71fc0 @base=#<mediaitem id: nil, title: "asdadasdasd", slug: "asdadasdasd", gallery_id: 1, created_at: nil, updated_at: nil>, @messages={:"photo_attributes.image"=>["select photo or video"], :"video_attributes.uid"=>["select photo or video"], :title=>[] }>
however these errors don't show in nested models. form looks this:
<div class="formwrapper"> <%= simple_form_for [:admin, @gallery, @media_item], html: { class: "#{action_name.titleize}photoform" } |f| %> <%= f.input :title %> <div class="photoinputs"> <h3>photo</h3> <%= f.simple_fields_for :photo |ff| %> <%= ff.input :image, as: :file_upload, input_html: {preview: @media_item.photo.image} %> <% end %> </div> <div class="videoinputs"> <h3>video</h3> <%= f.simple_fields_for :video |ff| %> <%= ff.input :provider, as: :radio_buttons, collection: video::providers %> <%= ff.input :uid %> <% end %> </div> <hr> <%= render partial: 'shared/form_submit', locals: {resource: @media_item} %> <% end %> </div>
why these inline errors failing show up?
note: there no absolutely no problem functioning of form. can create , edit both model , nested model without issue.
these 2 validations nil validates_presence_of :photo, allow_nil: true
validates_presence_of :video, allow_nil: true
because validating presence allowing not present. add together error
:base
, render in view. e.g.
validates :has_photo_or_video def has_photo_or_video if (photo.blank? ^ video.blank?) errors.add(:base,"please select photo or video") end end
then in view
<% f.errors[:base].each |message| %> <span>message</span> <% end %>
i have not seen implementation of inline errors describe although seek well:
def has_photo_or_video if !(photo.blank? ^ video.blank?) photo.errors.add(:image,"please select photo or video") video.errors.add(:uid,"please select photo or video") end end
although create no representation whether or not work.
your issue is looking errors contained in nested hash
{photo_attributes: {image: ["please select photo or video"]}, video_attributes: {uid: ["please select photo or video"]} }
the way have seen done validating on associated model not possible in case since 1 or both type procedure. @ to the lowest degree understand it.
if either or type procedure meaning mediaitem
either photo
or video
not both suggest looking polymorphism create procedure far easier.
or improve still scrap mediaitem
, add together title
attribute photo
, video
, create each 1 separately seems easiest route. (you still define method media_items
grab of these)
ruby-on-rails ruby forms ruby-on-rails-4 simple-form
No comments:
Post a Comment