Dislpaying validation errors from one one model to view of another.

13 views
Skip to first unread message

Sumit Srivastava

unread,
Dec 17, 2012, 11:30:08 PM12/17/12
to rubyonra...@googlegroups.com
Hi,

I have a model,

class UserComment
belongs_to :user
validates :player_id, :presence => true
validates :comment, :uniqueness => { :scope => :player_id}

partial "comments" from User.
= simple_form_for @user, :html => {:class => 'comment-form'}, :remote => true do |f|
  %fieldset
    = f.fields_for :user_comments do |uac|
      = uac.input :comment, :as => :text, :input_html => {:rows => 5, :width => 5}
      .comment-action
        = uac.button :submit, :value => 'Comment', :class => 'btn'


When I click on submit button and if the validation receives any error it does not display it on the view. I am not sure where the problem is.

AdofEssex

unread,
Dec 18, 2012, 4:24:16 AM12/18/12
to rubyonra...@googlegroups.com
It looks like you're not outputting your errors on your view.

If you go to your terminal/command line and try the following:

type 'rails c' to enter the rails console. Here we go do a quick bit of testing to see if the errors are being captured.
type 'user = User.new', this'll output a new User object.
type 'user.errors' If it returns Nil then you have a different problem otherwise it's a case of just displaying your errors.

In your view you could do something like...

<% if @user.errors %>
  <div class="errors">
    <p>There was an error, please see below.</p>
    <ul>
      <% @user.errors.full_messages.each do | msg | %>
        <li><%= msg %></li>
      <% end %>
    </ul>
  </div>
<% end %>

I hope that helps.

sumit srivastava

unread,
Dec 18, 2012, 4:28:57 AM12/18/12
to rubyonra...@googlegroups.com
On 18 December 2012 14:54, AdofEssex <adam.joh...@gmail.com> wrote:
It looks like you're not outputting your errors on your view.

If you go to your terminal/command line and try the following:

type 'rails c' to enter the rails console. Here we go do a quick bit of testing to see if the errors are being captured.
type 'user = User.new', this'll output a new User object.
type 'user.errors' If it returns Nil then you have a different problem otherwise it's a case of just displaying your errors.

In your view you could do something like...

<% if @user.errors %>
  <div class="errors">
    <p>There was an error, please see below.</p>
    <ul>
      <% @user.errors.full_messages.each do | msg | %>
        <li><%= msg %></li>
      <% end %>
    </ul>
  </div>
<% end %>

I hope that helps.



I was sending the errors to the view from the controller. I found out that since the errors where not from the base model (User) so it wasn't displaying them. Once I added them to the base using errors.add(:base, "Error"), it started displaying the errors.
 
On Tuesday, 18 December 2012 04:30:08 UTC, Sumit Srivastava wrote:
Hi,

I have a model,

class UserComment
belongs_to :user
validates :player_id, :presence => true
validates :comment, :uniqueness => { :scope => :player_id}

partial "comments" from User.
= simple_form_for @user, :html => {:class => 'comment-form'}, :remote => true do |f|
  %fieldset
    = f.fields_for :user_comments do |uac|
      = uac.input :comment, :as => :text, :input_html => {:rows => 5, :width => 5}
      .comment-action
        = uac.button :submit, :value => 'Comment', :class => 'btn'


When I click on submit button and if the validation receives any error it does not display it on the view. I am not sure where the problem is.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonra...@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-ta...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/1-jQOveMZ_8J.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Reply all
Reply to author
Forward
0 new messages