1

I have signup form on my home screen. If user inputs invalid data I redirect him to /signin page. On this page I can see filled fields, but errors descriptions are empty.

Here is my UsersController:

class UsersController < ApplicationController
  def new
    @user = User.new(params[:user])
  end

  def create
    @user = User.new(params[:user])
    print @user
    if  @user.save

    else
      render 'new'
    end
  end
end

Method I use to show errors

module ApplicationHelper

  def errors_for(model, attribute)
    if model.errors[attribute].present?
      content_tag :div, :class => 'well error' do
        content_tag :ul do
          model.errors[attribute].collect {|item| concat(content_tag(:li, item))}

        end
      end
    end
  end
end

My form partial:

<%= f.label :user_name %>
<%= f.text_field :user_name, :class=>"input-medium" %>
<%= errors_for(@user, :user_name) %>

<%= f.label :email %>

<%= f.text_field :email, :class=>"input-medium " %>

<%= errors_for(@user, :email) %>

<%= f.label :password %>

<%= f.password_field :password, :class=>"input-medium" %>

<%= f.label :password_confirmation, "Confirmation" %>

<%= f.password_field :password_confirmation, :class=>"input-medium" %>

and my signup view:

<section class="centered user-form-container">
  <div class="user-form well pull-left">
    <div class="centered">
      <h1>Sign up</h1>
      <%= form_for(@user, :action=>"create") do |f| %>
          <%= render 'signup', :f=>f %>
          <%= f.submit "Sign up" %>

      <% end %>
    </div>
  </div>

</section>
3
  • 1
    How does this code relate to your question? Commented May 14, 2012 at 13:17
  • I post data to create action of this controller and then renders new view template if there are some errors Commented May 14, 2012 at 13:21
  • 2
    you have to show us the view code. you must have some code that checks for errors in @user model and eventually show them Commented May 14, 2012 at 13:27

3 Answers 3

1

In this situation, I believe you need to use flash.now, something like this:

Per the rails docs:

By default, adding values to the flash will make them available to the next request, but sometimes you may want to access those values in the same request. For example, if the create action fails to save a resource and you render the new template directly, that’s not going to result in a new request, but you may still want to display a message using the flash. To do this, you can use flash.now in the same way you use the normal flash:

def create
  @user = User.new(params[:user])
  print @user
  if  @user.save

  else
    # start with this, then expand the error text
    flash.now[:error] = "Could not save user"
    render 'new'
  end

end
Sign up to request clarification or add additional context in comments.

5 Comments

I would like to show errors connected to fields next to this fields
No, if validation fails @user.errors gets set. That's what he wants to show.
The original question was unclear and didn't specify that -- so I misunderstood the question. My answer is accurate, it's just not what he was looking for. Downvoting seems harsh.
If you improve your answer I may undo my downvote, but from this answer it seems you are unfamiliar with validation errors and instead you are suggesting using the flash inappropriately. That's why I downvoted.
Thanks. My point was that the question as originally posed did not ask about validation errors. But I respect your opinion.
1

You would do this in your validation method.

If you are using a standard rails validation you would do this:

validates_presence_of :foo, :message => 'Message you want to display here'

If you are doing a custom validation then this:

def my_validation_method
  begin
    my_validation_code_here
  rescue
    self.errors[:base] << 'Message you want to display here'
  end
end

1 Comment

I have standart messages shown, if i try to sign up directly from signup page. I don't think custom messages will help me here
1
  def new
    @user = User.new(params[:user])
    if (!params[:user].nil?)
      @user.valid?
    end


  end

Comments

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.