Jump to content

The ultimate community for Ruby on Rails developers.


Photo

I'm stumped... displaying error messages


  • Please log in to reply
4 replies to this topic

#1 Rowel

Rowel

    Controller

  • Members
  • 109 posts

Posted 07 September 2013 - 11:02 PM

Help. I'm stumped and couldn't figure this out. I'll try to explain the problem as clearly as I can. 

 

Given: I have  a users page  (views/users/show.html.erb) 

http://localhost:3000/users/24

where 24 is user_id , for example

 

On this page, I'm displaying all his posts and some user profile information. These work fine. 

  # display user profile 
  def show
    @user = User.find(params[:id])
    # get this user's friends list
    @friends = @user.friends
    # get all posts by this user, with paging
    @posts = @user.posts.paginate(page: params[:page])
  end

On the same user page (view/users/show.html.erb), I also have an online form. 

<%= form_for(:post, url: posts_path, method: :post) do |f| %>
<%= render 'shared/error_messages'%>
...
<% end %>

It calls posts_path.  

and In my posts controller, I have this. 

  def create
   @post = current_user.posts.build(post_params)
   if @post.save
      # success
   end # @post.save
   redirect_to current_user
  end

This too, works fine... I can create new posts record, browser redirects to the user's page again, and my new post is listed on the screen. 

 

on Post, I have some model validations. They also work fine. 

validates :title,  presence: true,  length: { minimum:5, maximum: 140 }
validates :post,  presence: true, length: { minimum:5 } 

The problem is if there's an error, (for example, it didn't meet the field length requirements), I want to display an error message on the users page.  

 

The validations errors should be in @post.errors 

 

How can I display these @post.errors in the /users page?  

 

I tried this. This doesn't work...

 <%= render 'shared/error_messages'%>

_error_messages.html.erb

<% if @post.errors.any? then  %>
<p class="alert alert-error">Ooops! this is error message partial</p>
<% end #errors.any? %>


#2 Rowel

Rowel

    Controller

  • Members
  • 109 posts

Posted 08 September 2013 - 01:02 AM

I say to hell with it... :)

 

I just stored the error messages in a session variable, redirected back to the user page, and displayed the errors stored in the session variables. Works great, also gives me the ability to re-populate the form with the previous values. 

 

My Posts controller

  def create
   @post = current_user.posts.build(post_params)
   if @post.save
      # success saving new post
      session[:title]   = nil
      session[:request] = nil
     redirect_to current_user
    else
      # failure to save, errors encountered, let's store the list of error messages
      session[:errors] = @post.errors.full_messages
      # let's also store the current values of the online form so user doesn't have to re-enter
      session[:title]   = @post.title 
      session[:request] = @post.post
      redirect_to current_user
    end # @post.save  
  end

On my User's page I have this

          <% if session[:errors] %>
            <p class="alert alert-error">Ooops! Please correct the following: 
              <ul>
              <% session[:errors].each do |msg| %>
                  <li class="error"><%= msg %></li>
              <% end %>
              </ul> 
            </p>
            <% session[:errors] = nil %>
          <% end %>
if there are validation errors, display the error messages back to the user and let the user complete the missing fields in the form. His previous entry are preserved. 
        <%= f.label :title %>
        <%= f.text_field :title, placeholder: "Type a title or subject line (140 characters or less)", class: "input-xlarge", style:"width:95%", value: session[:title] %>

        <span class="help-block">Enter your request below:</span>
        <%= f.text_area :post, rows:"5", style:"width:95%", value: session[:request] %>
 


#3 Ohm

Ohm

    Guard

  • Members
  • 179 posts
  • LocationCopenhagen

Posted 08 September 2013 - 06:59 AM

You are on the right track. The Rails why to do it is to have the following in the controller:

def new
  @post = Post.new
end

def create
  @post = Post.new(params[:post])
  if @post.save
    redirect_to posts_path
  else
    render :new
  end
end

and then in the new.html.erb-file have

<%= form_for @post do |f| %>
  <%= render 'shared/error_messages', :object => @post %>
  <%= f.label :title %>
  <%= f.text_field :title, @post.title %>
  ...
<% end %>

and in shared/_error_messages.html.erb have

<% if object.errors.any? %>
  <p class="alert alert-error">Ooops!</p>
  <ul>
    <% object.errors.each do |msg| %>
      <li class="error"><%= msg %></li>
    <% end %>
  </ul> 
<% end %>

(Disclaimer: The above code might not work, as I just dotted it down into the reply box)


Blog: http://ohm.sh | Twitter: madsohm


#4 Rowel

Rowel

    Controller

  • Members
  • 109 posts

Posted 08 September 2013 - 02:09 PM

if @post.save
  redirect_to posts_path
else
  render :new
end

What if the Post form isn't in a Post View, but in a User view? And after a successful (or unsuccessful) posting, I want the user redirected back to User view, again not the post view. 

 

That was my problem.  I can display the error messages @post.errors fine inside a post view, but not within a user view.  I must be missing something here. 

 

Thanks. 



#5 Ohm

Ohm

    Guard

  • Members
  • 179 posts
  • LocationCopenhagen

Posted 08 September 2013 - 07:43 PM

Just make sure to have the @post-variable in your appropriate controller method and then redirect to the correct path on successful saving post and render the same view again if not, that is render the same user-view from the PostController if not successfully saved.


Blog: http://ohm.sh | Twitter: madsohm





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users