3

I have a rails app trying to incorporate some AJAX where clicking new opens a modal window and a form. I want to be able to display the validation errors if it fails so in my create action, i thought about re-rendering the new.js.erb file. Is this the right approach?

def create
    @place = Place.new(params[:place])
    if @place.save
       redirect_to places_path, :notice => "Successfully created place"
    else
       render "new.js.erb"
    end
end

The result I get is escaped js text in my browser like:

$("#new_grouping").html("<div class=\"modal-header\">\n  <a class=\"close\" data-   dismiss=\"modal\">×<\/a>\n  <h3>Create a new menu section<\/h3>\n<\/div>\n<form accept-charset=\"UTF-8\" action=\"/places/1-mama-s-pizza/groupings\" class=\"simple_form new_grouping\" id=\"new_grouping\" method=\"post\" novalidate=\"novalidate\">

I've tried putting various options into the render block but no luck. Any tips?

2 Answers 2

16

The best practice would be to support both, AJAX and Non-AJAX calls, in case the user has javascript turned off for any reason.

def create
  @place = Place.new(params[:place])

  respond_to do |format|
    if @place.save
      format.html { redirect_to places_path, :notice => "Successfully created place" }
      format.js   # renders create.js.erb, which could be used to redirect via javascript
    else
      format.html { render :action => 'new' }
      format.js { render :action => 'new' }
    end
  end
end

The render :action => 'new' actually renders the template of the controller action new which results to new.html.erb respectively to new.js.erb depending if it's a non-AJAX or an AJAX call.

In new.js.erb goes your ERB/javascript code:

$("#new_grouping").html("<%= escape_javascript(...) %>">
Sign up to request clarification or add additional context in comments.

2 Comments

good recommendation. I will definitely support both. However, it got me thinking, there is no way for me to achieve what I want which is to have a POST CREATE action respond with javascript rendered on my screen if it fails. It would have to be a redirect or render the entire page again. Is that fair to say?
Well yes and no. It depends how you write your new.js.erb and what you actually do in there. You could only insert some error specific HTML to certain elements of your page and not render the whole thing again. But in my experience you have to keep a lot of things in mind. So I actually wouldn't recommend to do a whole POST CREATE operation entirely with AJAX
1

As i know, rendering partial in controller is a bad idea, because then response can be without content-type and some browsers can't understand this. if it is some file attached to action you should write

render :action => "create"

or if you need just render a singe partial then in your action file write

<%= render :partial => "path/to/partial" %>

as i said, then you won't have problems with content-type in response

1 Comment

can't you just set the content type?

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.