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 8389017
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T18:36:26+00:00 2026-06-09T18:36:26+00:00

Is it possible to do model error reporting without a new method? It is

  • 0

Is it possible to do model error reporting without a new method? It is my understanding that when you want to report errors you would create the following controller code:

 def new
    @product = Product.new

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @product }
    end
  end

  def create
    @product = Product.new(params[:product])

    respond_to do |format|
      if @product.save
        format.html { redirect_to @product, notice: 'Product was successfully created.' }
        format.json { render json: @product, status: :created, location: @product }
      else
        format.html { render action: "new" }
        format.json { render json: @product.errors, status: :unprocessable_entity }
      end
    end
  end

And then you would create a form with the following code within it:

<% if @product.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@product.errors.count, "error") %> prohibited this product from being saved:</h2>

      <ul>
      <% @product.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

However, can you replicate this technique only using a ‘create’ method in the controller? For example, using a form_tag to pass data to the ‘create’ method. Maybe something like this:

‘post’, :class => ‘form-horizontal’, :style => ‘text-align:center’) do %>

            <% unless @product.blank? %>
              <% if @product.errors.any? %>
                <div id="error_explanation">
                  <h2><%= pluralize(@product.errors.count, "error") %> prohibited this product from being saved:</h2>

                  <ul>
                  <% @product.errors.full_messages.each do |msg| %>
                    <li><%= msg %></li>
                  <% end %>
                  </ul>
                </div>
              <% end %>
            <% end %>

               <%= text_area_tag :content, '', :placeholder => 'Ask your classmates anything...' %>
               <%= hidden_field_tag :user_id, current_user.id %>
               <%= submit_tag 'Post', :class => "btn btn-medium btn-primary post-room-button"  %>
            <% end %>

I have been trying to do this, but I cannot find a way to get it to work. Whenever I submit the form that fails one of the validations in my model, the page simply redirects to my ‘else’ path in my respond_to function in my create controller.

  • 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-09T18:36:28+00:00Added an answer on June 9, 2026 at 6:36 pm

    You can remove the compete new method from your controller but not the new view present in the views file.

    When you say render action: “new” it renders the new.html.erb file present in the views folder of your app and when you say redirect_to action: “new” it will call the new action of the controller and will perform the operations present in the new action and then render the new.html.erb.

    You cannot render the create action as it does not contain any view file and create action as per REST is supported by POST request and not GET request

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

Sidebar

Related Questions

Possible Duplicate: Error : get property of non-object I am new to codeigniter model,
Is it possible to add model state error message during updating properties using TryUpdateModel?
I want DRY/reuse as much editor code (View and Model) as possible. Some of
it isn't possible to call a model's methods in a template without making your
Is it possible to query data from a Model without writing sqarql-queries? Adding properties
Is it possible to create a SQL DB from a Linq2Sql model? I managed
Possible Duplicate: Model.find(1) gives ActiveRecord error when id 1 does not exist If there
I'm using a third party component that I call a method passing a model
Without having the full module path of a Django model, is it possible to
So I am trying to figure out a method that can render a error

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.