On Jun 26, 2012, at 4:23 PM, cyber c. wrote: > Hi, > > Thanks for the suggestions. I have now included the code to display the > error messages. Here is the code for my MVC > > model > class Record > include ActiveModel::Validations > validates: :ipaddress, :name, :presence => true > end > > View > > <%= form_for :Record, :url => {:action => 'submit'} do |f| %>
Leave off the {:action} part here, I'll explain why in a moment > <%= f.error_messages %> > <div class="field"> > <%= f.label :ipaddress %><br /> > <%= f.text_field :ipaddress %> > </div> > <div class="field"> > <%= f.label :name %><br /> > <%= f.text_field :name %> > </div> > <div class="actions"> > <%= f.submit "Submit"%> > </div> > <% end %> > > Controller > Somehow this gets routed to action "create" instead of "submit" when the > submit button is called. Not sure why it is If your routes look like resources :yourmodel Then you are using REST URLs, and a POST from your form will always go to #create, and a PUT (which you will automatically get from the form_for if your resource has an id) will always go to #update. If you want to use the submit method in place of create, you can probably do that in your routes file, but why? > > def create -> same for def submit too > > respond_to do |format| > if PARAM matches so and so > DEPENDING on params this gets routed to different page at any point here do you instantiate a new instance of your model, assign the params to it, and try to save it? Until you try to save, your validations won't fire. > else > format.html { redirect_to records_url} > format.json { head :no_content } > end > end > > end > > Should i write my controller differently to handle the errors? You might want to look at a vanilla scaffold controller and see how it's done canonically before you reinvent the wheel. There are separate create and update methods for a reason. Walter > > -- > Posted via http://www.ruby-forum.com/. > > -- > 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 rubyonrails-talk@googlegroups.com. > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscr...@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > -- 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 rubyonrails-talk@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-talk+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.