11

In a model say Task, I have following validation

 validates_presence_of :subject, :project, :user, :status

How do I render error messages for these validations using some other controller.

Inside CustomController I am using,

Task.create(hash)

passing nil values in hash gives following error,

 ActiveRecord::RecordInvalid in CustomController#create

 Validation failed: Subject can't be blank

Code in the controller's create action

@task = Task.create(hash)
if @task.valid?
   flash[:notice] = l(:notice_successful_update)
else
    flash[:error] = "<ul>" + @task.errors.full_messages.map{|o| "<li>" + o + "</li>" }.join("") + "</ul>"
end
redirect_to :back

How to render error messages to user.

Task model is already built and it renders proper messages. i want to use its functionalities from a plugin.

4
  • 1
    you are also saving it twice in this create action. create will save it, and then you save it again. Commented May 10, 2012 at 12:18
  • oh! thnx for bringing it to notice. Commented May 10, 2012 at 12:39
  • @stellard, updated the post with the solution that worked for me. i guess this is not a bad approach. comments please? Commented May 10, 2012 at 12:42
  • I definitely would not put any html in the controller. This should be in the views, just the message text should be in the controller. It doesn't follow rails conventions, but without getting into more details, its generally ok. Commented May 10, 2012 at 22:20

2 Answers 2

8

This should work

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

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

  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>
Sign up to request clarification or add additional context in comments.

8 Comments

you could turn this into a helper to replicate the old error_messages_for functionality
okay. i will give this a try. i wanted to use 'error_messages_for' because the i am working on redmine plugins and redmine seems to use 'error_messages_for'.
error_messages_for is available. But you have to use this gem: github.com/joelmoss/dynamic_form
@stellard, I tried your solution, but i am still getting above error ActiveRecord::RecordInvalid in CustomController#create Validation failed: Subject can't be blank
is it because i am calling Create method from other(i.e not taskscontroller) controller?
|
1

If you're talking about showing errors on the edit-page (i.e.: in the form), then consider usage of the simple_form gem. It'll show errors for you without a line of coding. Just replace form_for with simple_form_for and the magic will begin...

6 Comments

can you suggest some other solution? that doesnt require installing gem
what i have posted is what error messages for does. it was removed in rails 3
@stellard, I know about this. I just don't see any alternatives for gems. And error_messages_for wasn't just removed but was placed into a separate gem (github.com/joelmoss/dynamic_form). So I think the author is doomed to use gems unless he wants headache while building forms.
@jdoe Doomed is pretty extreme I think... One could easily turn what I posted into a helper and use that on all their forms. They would also be able to customise the styling as well, which is more difficult if a gem is used.
@stellard, agreed to your last comment.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.