On 26 okt, 20:57, pharrington <[email protected]> wrote:
> On Oct 26, 2:12 pm, Ronald <[email protected]> wrote:
>
> > I have a page displaying a form and a table. The form is used to add
> > record to the table using ajax.
> > I am using jQeury for this.
> > Submitting the form with all fields filled in works just fine, but
> > when I leave field empty it does not work.
>
> What does not work? Is the Project not created? Are you getting an
> exception (and if so, what is the full exception + backtrace)?

When I submit the empty form nothing happens in the browser. The
project is not created also no exception is raised.
On the project model I defined these validations:
  validates_presence_of :name
  validates_presence_of :description

>
> > Probably because the jQuery code tries to add a partial (containing
> > one row) to to the bottom of the table for the new object that could
> > not be created because fields where empty.
>
> Have you checked the development log to see if extraneous, blank
> fields actually are being sent in the POST request?
Yes, blank fields are send in the POST request.

In the development log i found this:
ActionView::TemplateError (edit_project_url failed to generate from
{:controller=>"projects", :action=>"edit", :id=>#<Project id: nil,
name: "", description: "", created_at: nil, updated_at: nil,
startdate: "2009-10-26", enddate: "2009-10-26">}, expected:
{:controller=>"projects", :action=>"edit"}, diff: {:id=>#<Project id:
nil, name: "", description: "", created_at: nil, updated_at: nil,
startdate: "2009-10-26", enddate: "2009-10-26">}) on line #12 of app/
views/projects/_project.html.erb:
9:      <td><%=h project.enddate %></td>
10:     <td class="row-nav">
11:             <%= link_to "Show", project %>
12:             <%= link_to "Edit", edit_project_path(project) %>
13:             <%= link_to "Destroy", project, :confirm => 'Are you
sure?', :method => :delete %>
14:     </td>
15: </tr>

This is there because my javascript code in create.js.erb that gets
executed on the format.js response tries to render that _project
partial in my view.

>
> > My create action now looks like this:
> >   def create
> >     @project = Project.new(params[:project])
> >     @project.save
> >     @current_project = @project
> >     flash[:notice] = 'Successfully created project.'
> >     respond_to do |format|
> >       format.js
> >       format.html { redirect_to projects_url }
> >     end
> >   end
>
> You have no error checking in your controller, so if you're wondering
> why there's no validation in your app, this is a good place to start.
> Readhttp://guides.rubyonrails.org/activerecord_validations_callbacks.html,
> its an excellent Primer for how ActiveRecord validations work and how
> to work with them in your controller code and views.

Thanks for the link.

Of course I can change the create action something like this:
  def create
    @project = Project.new(params[:project])
    if @project.save
      @current_project = @project
      flash[:notice] = 'Successfully created project.'
      respond_to do |format|
        format.js
        format.html { redirect_to projects_url }
      end
    else
      flash[:notice] = 'Failed to create a new project.'
      render :action => 'index'
    end
  end

But how do I show the validation messages on the index view?
In my view I have this:
<%= error_messages_for :project %>
But it does not show anything when I submit an empty form with the
create action code from just above.
Also the 'Failed to create a new project.' does not show.
--~--~---------~--~----~------------~-------~--~----~
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 [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to