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.

hitesh israni
  • 1,742
  • 4
  • 25
  • 48
  • 1
    you are also saving it twice in this create action. create will save it, and then you save it again. – stellard May 10 '12 at 12:18
  • oh! thnx for bringing it to notice. – hitesh israni May 10 '12 at 12:39
  • @stellard, updated the post with the solution that worked for me. i guess this is not a bad approach. comments please? – hitesh israni May 10 '12 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. – stellard May 10 '12 at 22:20

2 Answers2

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 %>
stellard
  • 5,162
  • 9
  • 40
  • 62
  • you could turn this into a helper to replicate the old error_messages_for functionality – stellard May 10 '12 at 10:29
  • 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'. – hitesh israni May 10 '12 at 10:32
  • `error_messages_for` is available. But you have to use this gem: https://github.com/joelmoss/dynamic_form – jdoe May 10 '12 at 10:36
  • @stellard, I tried your solution, but i am still getting above error ActiveRecord::RecordInvalid in CustomController#create Validation failed: Subject can't be blank – hitesh israni May 10 '12 at 10:47
  • is it because i am calling Create method from other(i.e not taskscontroller) controller? – hitesh israni May 10 '12 at 10:48
  • Are you using the create! method? That will raise an exception. Use create which will not raise a validation error and fall through to your view to render to the user. – stellard May 10 '12 at 10:53
  • i changed it to create instead of create! but instead of getting validation errors i get successful update flash. please check the updated code in the post – hitesh israni May 10 '12 at 11:10
  • i will update the post with the code that worked for me. your pointers/hints helped me. thnx – hitesh israni May 10 '12 at 11:32
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...

jdoe
  • 15,665
  • 2
  • 46
  • 48
  • can you suggest some other solution? that doesnt require installing gem – hitesh israni May 10 '12 at 10:15
  • what i have posted is what error messages for does. it was removed in rails 3 – stellard May 10 '12 at 10:25
  • @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 (https://github.com/joelmoss/dynamic_form). So I think the author is doomed to use gems unless he wants headache while building forms. – jdoe May 10 '12 at 10:29
  • @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 May 10 '12 at 10:32
  • @stellard, agreed to your last comment. – hitesh israni May 10 '12 at 10:33
  • @jdoe, +1 for suggesting an alternative – hitesh israni May 10 '12 at 11:31