f.error_messages in Rails 3.0

匿名 (未验证) 提交于 2019-12-03 07:50:05

问题:

Rails 3.0 deprecated f.error_messages and now requires a plugin to work correctly - I however want to learn how to display error messages the (new) native way. I am following the getting started guide, which uses the deprecated method when implementing the comments form. For example:

Add a comment:



Here is the correct way to do it (as generated by the scaffold):

        

prohibited this post from being saved:

. . .

I understand that I use the @post variable in the latter example, but what variable do I reference in the former to get the error messages for comment creation?

回答1:

I just looked into the docrails github issues, and they've decided to remove f.error_messages instead of explaining how to do validation for comments.



回答2:

The best and clean way to implement error_messages in your form is by implementing error_messages in a FormBuilder.

For example, here is the error_messages method I've implemented for my last project. By implemeting your own FormBuilder you can follow the rules and styles of your webdesigner... Here is an example that will output the errors list in ul/li's with some custom styles :

class StandardBuilder  "title-error")     errors_list  "error-recap round-border")   end end

Then in my forms :

= f.error_messages

And that's all.



回答3:

I'm pretty sure all you'd need to do is reference @post.comments

So you could do something like:

                    
  • Or just pull all the errors out out:

    comment_errors = @post.comments.map(&:errors)

    and then loop through them in your display logic to output each of the comment errors.



    回答4:

    This functionality exists as a standalone gem dynamic_form.

    Add the the following to your Gemfile

    gem 'dynamic_form'

    From the github page:

    DynamicForm holds a few helpers method to help you deal with your Rails3 models, they are:

    • input(record, method, options = {})
    • form(record, options = {})
    • error_message_on(object, method, options={})
    • error_messages_for(record, options={})

    It also adds f.error_messages and f.error_message_on to your form builders.



    回答5:

    Here is my solution to the whole error scene.

    I created a partial which simply uses a model variable which one would pass when rendering it:

              

    You can easily add dynamic html class and/or id names based on the name of the model, as well as generic ones.

    I have things setup where my error messages render in all the same place in a layout file:

      

    If one didn't want that functionality, removing the content_for in the partial would do the trick.
    Then in really any view you want you can simply write:

     

    One could further expand this by creating a partial which takes a collection and leverages the error partial above:

          

    Render it with:

     

    I like this way. It is simple, easy to manage/maintain, and incredibly tweakable.
    I hope this helps!

    EDIT: Everything in HAML

    -# app/views/errors/_error.html.haml  = content_for :message do   - if model.errors.any?     %ul       - model.errors.full_messages.each do |msg|         %li= msg


    -# app/views/layouts/application.html.haml  = yield :message


    = render 'errors/error', model: @some_model


    -# app/views/errors/_collection.html.haml  - collection.each do |model|   = render 'errors/errors', model: @some_model


    = render 'errors/_collection', collection: @some_model.some_has_many_association


    回答6:

    I guess that the [@post, @post.comments.build] array is just passed to polymorphic_path inside form_for. This generates a sub-resource path for comments (like /posts/1/comments in this case). So it looks like your first example uses comments as sub-resources to posts, right?.

    So actually the controller that will be called here is the CommentsController. The reason why Lukas' solution doesn't work for you might be that you actually don't use @post.comments.build inside the controller when creating the comment (it doesn't matter that you use it in the view when calling form_for). The CommentsController#create method should look like this (more or less):

    def create   @post = Post.find(params[:post_id]   @comment = @post.comments.build(params[:comment])    if(@comment.save)     # you would probably redirect to @post   else     # you would probably render post#show or wherever you have the form   end end

    Then you can use the code generated by scaffolding, only replace @post instance variable with @comment in all the lines except form_for call.

    I think it may also be a good idea to add the @comment = @post.comment.build to the controller method that displays this form and use form_for([@post, @comment], ...) to keep the form contents displayed in the form if there're errors.

    If this doesn't work and you're not able to figure it out, please add your CommentsController#create method to the question.



    转载请标明出处:f.error_messages in Rails 3.0
    标签
    易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
    该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!