Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8413151
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T00:51:52+00:00 2026-06-10T00:51:52+00:00

My validations are not working for a nested form – messages, which is in

  • 0

My validations are not working for a nested form – messages, which is in other models show page.

Here’s the code:

Reserve Online:

   <%= form_for([@trip, @trip.messages.build]) do |f| %>    

      <%= render 'shared/error_messages', object: f.object %>  

      <%= f.text_field :name, :class => "span3", :placeholder => "Name:" %>     
      <%= f.text_field :email, :class => "span3", :placeholder => "Email:" %>   
      <div class="h">    
        <%= f.text_field :subject, :class => "h", :value => (@trip.title) %>   
      </div>  

      <%= f.text_area :body, :class => "input-xlarge3", :placeholder => "Message:", :id => "textarea", :rows => "3" %>

      <%= f.submit :class => " btn btn-primary btn-large ", :value => "Send Message" %>    
  <% end %>                                
</div>

Message.rb

class Message < ActiveRecord::Base

 belongs_to :trip  
 attr_accessible :name, :email, :subject, :body 

 validates_presence_of :name
 validates_format_of :email, :with => /^[-a-z0-9_+\.]+\@([-a-z0-9]+\.)+[a-z0-9]{2,4}$/i
 validates_length_of :body, :maximum => 500

end

an the messages_controller.rb

 class MessagesController < ApplicationController

   def create
      @trip = Trip.find(params[:trip_id])
      @message = @trip.messages.create(params[:message])     
   if @trip.messages.create
      MessageMailer.send_message(@message).deliver
      redirect_to thank_you_path        
   else    
      redirect_to trip_path(@trip)
   end
 end
end

_error_messages.rb

<% if object.errors.any? %>
  <div id="error_explanation">
   <div class="alert alert-error">
    The form contains <%= pluralize(object.errors.count, "error") %>.
   </div>
   <ul>
   <% object.errors.full_messages.each do |msg| %>
     <li>* <%= msg %></li>
   <% end %>
   </ul>
 </div>
<% end %>

The whole code above works fine, but the validation part is simply ignored. And I don’t see any errors.
So I Can’t figure out what I’m doing wrong. Can You help?

Thank you!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T00:51:53+00:00Added an answer on June 10, 2026 at 12:51 am

    I think there is a problem in your controller:

    def create
      @trip = Trip.find(params[:trip_id])
      @message = @trip.messages.create(params[:message])     
      # the following line doesnt make sense
      # you're recreating an empty message
      # it should be something like "if @message.valid?"
      if @trip.messages.create
        MessageMailer.send_message(@message).deliver
        redirect_to thank_you_path        
      else    
        redirect_to trip_path(@trip)
      end
    end
    

    Try to fix that and see if it helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The whole code is from this question: Nested form validations not working in rails
Validations are working but form is not getting submitted and data is not inserted:
My javascript is not working right. It is simple pre-vailidation form and I can
I have the following C# code. Here the validations are kept outside the class
Validations is not working when I use select tag in rails if i remove
Here is my html code with javascript validations.... HTML elements are showing well javascript
http://loopj.com/jquery-tokeninput/ The code I am working with is from a jquery-tokeninput fork which allows
I find that the validation rule 'inList' in cakephp 1.3.10 is not working properly.
I have a problem with validation messages not showing after a redirect, even when
alt text http://img42.imageshack.us/img42/4161/blinkthru.png I'm trying to get the validation to not show through my

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.