views:

30

answers:

2

So, each rails project i seem to run into this problem and would be really grateful if someone could enlighten me:

With a "normal" setup when the form sits on the view immediately associated with the url/controller it is pretty straightforward, a render => :action on fail will display the validation message.

Now what i have is a form in a partial sitting on a page whose url/controller is a show/:id and not the create action of the form, the validation kicks in but i cannot get the validation message to display because i can't trigger the correct render action... CLosest i got is a render => @object but there is no css/layout, i can pass a message through a redirect with flash[] but it feels wrong, same with jquery/client error messages...

So how can i "cleanly" display validation messages of a form in a partial (under another controller/action than the parent page)?

(thanks in advance for your help)

edit: can't paste the actual thing now but i'll do my best to explain

i have a main page e.g. article/show/01, on this page is the content of the article (@article) and then at the bottom of the page is a partial _commentform with a form to post a comment. This form is bound to a Create action of a different controller (comments controller).

Now if the form were on its own "page"/url instead of a partial, say /comment/create, i would jus do:

if @comment.save
redirect_to @comment
else 
render => :create
end

and the validation would display normally.

In my case the form is in a a partial on the article/show/01 url, what should be the equivalent to the code above so that on validation fail error messages are displayed on the parent url, like "render article/show/01" ? I am sure it is easy but i cannot get it to work (i just can redirect on success but cannot display the errors with a render)

A: 

Hello!

I don't think the best way to display validations errors is to render a partial.

IMHO, the best and clean way to display errors messages using the styles/css you or your webdesigner wants is by implementing your own error_messages method in a FormBuilder.

For example, here is the error_messages method I've implemented for my latest project.

Here is an example that will output the errors list in ul/li's with some custom styles... Just customize this and put your form builder in app/helpers...

class StandardBuilder < ActionView::Helpers::FormBuilder
  def error_messages
    return unless object.respond_to?(:errors) && object.errors.any?

    errors_list = ""
    errors_list << @template.content_tag(:span, "There are errors!", :class => "title-error")
    errors_list << object.errors.full_messages.map { |message| @template.content_tag(:li, message) }.join("\n")

    @template.content_tag(:ul, errors_list.html_safe, :class => "error-recap round-border")
  end
end

Then in my forms :

= form_for @post, :builder => StandardBuilder do |f|
  = f.error_messages
  ...

No need to display/render another partial. And that's all :).

slainer68
Hi, thanks for your answer, i am not trying do display validation errors in a partial, it is just that the form in itself is in a partial and i just would like to display validation errors for it in a simple/standard manner. Not sure i completely understood your method but it would still need to render the form partial to dsiplay this custom message, i am stuck because i cannot do that...
Monza
I don't understand your problem. You mean that all your form is in a partial and you want to access your validations errors elsewhere? Please gist/paste your main page and your form partial. Thanks.
slainer68
I have edited the original post to include more details. Thanks!
Monza
A: 

Hi Monza

If you want to display anything (including error messages) in a partial you have two ways

1 - Define it in the controller action where the partial is called 2 - pass the message as a parameter to the partial

1 - Example

in your controller/action

if @comment.save
 redirect_to @comment
else
 @messages = "This is a message" 
 render => :create
end

in your partial

you can access the @message variable

2 - passing the variable to the partial

render :partial => “<partial name>”, :locals => { :param1 => “value”}

<partial name> – name of your partial (Ex /partials/footer)

:params1 – parameter name

“value” – value

hope this helps

thanks

sameera

sameera207