Topic: displaying validation error message in from

Hi everyone, I am struggling on how to display model validation error message in my view of parent model for a form based on child model.
I have two models
tickets
has:many ticketlogs

ticketlogs
belongs_to ticket

in the show view of ticket I am displaying some ticket information and then creating a child form to add multiple ticketlogs for the ticket

<%  form_for [@ticket, Ticketlog.new do |f| %>
      <%= f.error_messages %>
    <p>
      <%= f.label :Notes %><br />
      <%= f.text_area :activity ,"rows" => 10 , "cols" => 60 %>
    </p>
    <p>
      <%= f.label :ticket_id %><br />
      <%= f.text_field :ticket_id%>
    </p>
    <p>
      <%= f.submit "Create" %>

in my ticketlog model I have made :activity as a required field. but for some reason when I ry to create a ticketlog without activity , it doesn';t create it but doesn't show any error messages either in spite of having    <%= f.error_messages %>
code from my ticketlog controller
def create
    @ticket = Ticket.find(params[:ticket_id])
     @ticketlog = @ticket.ticketlogs.new(params[:ticketlog])
     respond_to do |format|
       if @ticketlog.save
      flash[:notice]= 'Your Ticket note was created successfully'
      format.html { redirect_to ticket_url(@ticketlog.ticket_id)}
      format.xml { render :xml => @ticketlog, :status => :created, :location => @ticketlog }
       else
       
      format.html { redirect_to @ticket }
       end
     end

thanks for our help here

Last edited by techy (2009-05-14 17:21:51)

Re: displaying validation error message in from

In your else block, when the ticketlog does not save, you do not want to redirect, you want to re render the new action.  It should look like

respond_to do |format|
  if @ticketlog.save
    flash[:notice]= 'Your Ticket note was created successfully'
    format.html { redirect_to ticket_url(@ticketlog.ticket_id)}
    format.xml { render :xml => @ticketlog, :status => :created, :location => @ticketlog }
  else     
    format.html { render :action => "new" }
    # instead of
    # format.html { redirect_to @ticket }
  end

You should then see the error messages

Re: displaying validation error message in from

Thank you so much for your reply, I have tried your option but the issue with that is it takes the user to the ticketlogs -new page, our requirement is to display message in the ticket show page itself where the child form is.

is there a way to achieve this i.e capture the error_messages_for :tickelog and display in the ticket show view / page
please advice

Re: displaying validation error message in from

You can render whichever view you would like.  You just have to make sure you have declared all the needed variables that will be used in the other action.  You should be able to do this...

format.html { render :controller => "tickets", :action => "new" }

...and as long as you have the correct form with the error_messages method, it should work just like you want it to

Re: displaying validation error message in from

Thank you again .. i am sorry if I didn't clarify the issue clearly
I have two controller tickets and ticketlogs

in the tickets I have a a view show where I am showing ticket details and in the bottom I got a form_for ticketlogs

- I would like the ticketlog validation errors to be appear in the ticket controller show view

so now even in my ticketlog controller edit method I say
    format.html { render :controller => "tickets", :action => "new" }

it goes to the controller ticketlogs new view

Please advise

Re: displaying validation error message in from

The rails api isn't very clear about rendering to another controller and when I tested it out on my own, it didn't render a different controllers views with the :controller option. But this did work to render to another controller and in your case it'll probably look like...

format.html { render :action => "../tickets/show" }

It doesn't seem very clean, but it works.

Re: displaying validation error message in from

Thank you for your update , I have tried this and getting this error:

Missing template ticketlogs/../tickets/show in view path C:/demo/sticket/app/views:

seems like its trying to look for tickets folder under ticketlogs folder not sure why

thanks

Re: displaying validation error message in from

render :action => "tickets/show"

when a slash is present in the path, rails searches for this path from the /view/ folder, so there's no need for /../ tricks

Re: displaying validation error message in from

Hi Sorry for the late reply, I tried again with ticket/show and got same results

Missing template ticketlogs/tickets/show.erb in view path C:/demo/sticket/app/views:

as you can see  the render function is going to ticketlogs folder

Re: displaying validation error message in from

Did you ever resolve this issue? I'm trying to do the same thing...

 class CommentsController < ApplicationController  
  def create
    @post = Post.find(params[:post_id])
    @comment = @post.comments.new(params[:comment])
    respond_to do |format|
      if @comment.save
        format.html { redirect_to(@post) }
        format.js
      else
        format.html { render :template => "posts/show", :layout => "default" }
      end
    end
  end   
end 

and then in "posts/show"

<h2>Leave a Reply</h2>
<% remote_form_for [@post, Comment.new] do |f| %>
  <%= f.error_messages %>
  <div class="grid_1 alpha">
    <%= f.label :name, "Name" %>
  </div>
...

I have a simple validation on the comment model

class Comment < ActiveRecord::Base
  belongs_to :post
  validates_presence_of :name
end

When I submit the form without a name the comment built in memory cannot be saved due to validation errors and the posts/show template is rendered however the error messages aren't being displayed either. What was your fix for this?

Re: displaying validation error message in from

Alexandermillar,

in "posts/show", replace the following line:

<%= f.error_messages %>

For

<%= error_messages_for 'comment' %>

Last edited by bmsatierf (2010-11-24 08:54:50)