SitePoint Sponsor

User Tag List

Results 1 to 16 of 16

Hybrid View

  1. #1
    SitePoint Zealot
    Join Date
    May 2007
    Posts
    121
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    error_messages_for on Rails 2.0.2

    Hello, does something change on Rails 2.0.2 about the using of error_messages_for helper ???

    I have just started a new project on 2.0.2 (the first one with this Rails version) and I did, like always

    app_controller
    Code:
      def add_category
          @category = Category.new(params[:category])
    
          if request.post? and @category.save
            flash[:notice] = 'Category successfully created'
          end
          
           redirect_to :action => "settings" 
      end
      
    end
    settings.html.erb
    Code:
    <%= error_messages_for "category" %>
    But it doesn't show any error message.

    Thank you
    Alla prossima ...

  2. #2
    SitePoint Addict nueva's Avatar
    Join Date
    Apr 2006
    Location
    Uk, London
    Posts
    228
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    that's strange, it should work.......let me have a better look on some documentation........

  3. #3
    SitePoint Zealot
    Join Date
    May 2007
    Posts
    121
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks
    I appreciate it
    Alla prossima ...

  4. #4
    SitePoint Addict nueva's Avatar
    Join Date
    Apr 2006
    Location
    Uk, London
    Posts
    228
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you are welcome.....

  5. #5
    SitePoint Zealot
    Join Date
    May 2007
    Posts
    121
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Did you find something about this issue ???


    Thanks buddy
    Alla prossima ...

  6. #6
    SitePoint Addict nueva's Avatar
    Join Date
    Apr 2006
    Location
    Uk, London
    Posts
    228
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    nope, not at the moment, sorry........

  7. #7
    SitePoint Zealot
    Join Date
    May 2007
    Posts
    121
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks buddy.

    Someone else has some idea ???
    Alla prossima ...

  8. #8
    SitePoint Addict nueva's Avatar
    Join Date
    Apr 2006
    Location
    Uk, London
    Posts
    228
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    no problem mate, if you want i can give u some documentation, maybe you can find the error....

  9. #9
    SitePoint Zealot
    Join Date
    May 2007
    Posts
    121
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks but I have already checked it out.

    I hope someone can help me.

    BYE
    Alla prossima ...

  10. #10
    SitePoint Addict nueva's Avatar
    Join Date
    Apr 2006
    Location
    Uk, London
    Posts
    228
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    def add_category
      @category = Category.new(params[:category])
     
      if request.post? and @category.save
        flash[:notice] = 'Category successfully created'
      end
        
      redirect_to :action => "settings"
    end
    end
    You have two ends in your def? Maybe that is it?
    The error_messages_for is not an error it is code that is located in your view.
    Is it not working as expected?

    Let me know.....

  11. #11
    SitePoint Zealot
    Join Date
    May 2007
    Posts
    121
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There are 2 end simply because the second is the controller end.

    It doesn't work at all, for instance I validate the presence of the "name" field and when I submit the form without name, the values are nor saved but no message is showed.


    Thanks
    Alla prossima ...

  12. #12
    SitePoint Evangelist
    Join Date
    Feb 2006
    Location
    Worcs. UK
    Posts
    404
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by macsig View Post

    settings.html.erb
    Code:
    <&#37;= error_messages_for "category" %>
    But it doesn't show any error message.
    What do you get if you put that code in the view template that contains the input form? It might also be worth trying a symbol for category rather than a string:
    Code:
    <%= error_messages_for :category %>
    Often you can use either, but sometimes a symbol will work where a string won't.

  13. #13
    SitePoint Zealot
    Join Date
    May 2007
    Posts
    121
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks I tried both symbol and string with the same result: nothing is showed.

    The helper is already in the same view with the form.


    Code:
    <div class="admin_form_wrap">
    	<&#37; form_for :category, :url => { :action => :add_category}, :html => { :id => "main_form"}  do |form| %>
    		<div class="element_wrap">
    			<span id="name">
    				<label for="category_name" class="mandatory_field_name">new</label>
    				<%= form.text_field :name, :class => "text_field" %>
    		</div>
    		<div class="admin_button_wrap"><a class="submit_button" href="#" onclick="submitform('main_form')"><span>add</span></a></div>	
    	<% end %>
    </div>
    
    <div id="error">
    <%= error_messages_for :category %>
    </div>
    Thanks
    Alla prossima ...

  14. #14
    SitePoint Addict nueva's Avatar
    Join Date
    Apr 2006
    Location
    Uk, London
    Posts
    228
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In your controller, you need to tell the action to rerender the page, but your code, regardless of state always does a redirect, which means it reinitiates your object, losing your errors.

    To solve it you need something like this:

    Code:
    def add_category
      @category = Category.new(params[:category])
     
      if request.post? and @category.save  # here I have succeeded and can move on
        flash[:notice] = 'Category successfully created'
        redirect_to :action => "settings"
      else  # here I rerender the page to show the errors captured in the @category object
        flash[:error] = 'Category could not be created'
        render :action => 'add_category'
      end    
    end

  15. #15
    SitePoint Zealot
    Join Date
    May 2007
    Posts
    121
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks buddy
    I solved it 10 minutes ago
    Alla prossima ...

  16. #16
    SitePoint Addict nueva's Avatar
    Join Date
    Apr 2006
    Location
    Uk, London
    Posts
    228
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you are welcome, glad i could help...


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •