1

I'm still relatively new to rails and getting used to a few things. Just a quick question really, I have an AJAX form (with :remote => true etc...) which works and sends the data to the server without a pageload.

My question is around the javascript file that can be used after the post. My partial is called '_newsomething.html.erb' and once it's posted I want to execute a javascript file - do these files have to be named after actions in the controller to sync and work properly in rails? At the moment I have a javascript file called 'create.js.erb' with just an javascript alert in it, but it's not firing.

e.g. do I need to have a partial called '_create.html.erb' and then a javascript file called 'create.js.erb'? Is there anyway of telling Rails where to go for a javascript file from within the controller?

Thanks in advance!

EDIT: Controller code below (very basic and generated at the moment)

  def create
@snip = Snip.new(params[:snip])

respond_to do |format|
  if @snip.save
    format.html { redirect_to(@snip, :notice => 'Snip was successfully created.') }
    format.xml  { render :xml => @snip, :status => :created, :location => @snip }
  else
    format.html { render :action => "new" }
    format.xml  { render :xml => @snip.errors, :status => :unprocessable_entity }
  end
end

end

1
  • It might be helpful if you include your controller code. Specifically the create action that is getting called when you submit the remote form. Commented May 31, 2011 at 19:20

1 Answer 1

2

In your controller action you have to have:

respond_to do |format|
  format.html { .... }
  format.js
end

The format.js is the important part. If Rails discovers that you are POSTing through AJAX, then it will render _create.js.erb partial.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks mate - needed to make a couple of other minor tweeks but this did the trick, lovely stuff :).

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.