Save but don' redirect,

Rails 3.1.3

I have Video, Script classes having

Video 1----0…n Script


in “Video” show.html.erb, there are both

<%= render :partial => “new_script”,
:locals => { :script => =>} %>


<%= render :partial => "script_list", :locals => {:scripts =>

@video.scripts} %>

in other words, a single page contains a video clip and “script” create
form as well as the list of all “scripts” that belong to that video.

My question is :
How can I stay in that page even after clicking the save button for a
new script?
And How can I update the “Script” list in the Video page using ajax?

Originally, scaffold generated

def new
@script =
respond_to do |format|
format.json { render json: @script }

As all of you are aware, this method redirect_to Script “show” page. I
tried for “_new_script.html.erb” partial, I added

  <%= link_to 'SAVE', {:controller => 'scripts', :action => 'new' },

and for scripts_controller.rb

respond_to do |format|
  format.html { redirect_to :video }   #changed HERE
  format.json { render json: @script }

But does not do the job.

jQuery is working properly, so the problem of ajax part is just coding.

Could anyone help me out?

Thanks in advance.


On Fri, Feb 3, 2012 at 00:52, Soichi I. [email protected]

How can I stay in that page even after clicking the save button for a
new script?

In your controller, you have control over what page gets rendered.
When you create a new script, attached to a video, you can simply have
the successful creation redirect to showing the video, or render that
view explicitly.


Dave A.: Available Cleared Ruby on Rails Freelancer
(NoVa/DC/Remote) – see, and blogs at,,

Thanks for the reply. Like you pointed out, I have been
misunderstanding that redirecting was due to ‘new’ action. It’s rather
‘create’ action.

Right now I am trying to develop jQuery ajax that updates a part of the

So I tried in script controller,

def create
@script =[:script])
respond_to do |format|
format.html { notice: ‘Script was successfully added.’ }
format.js do
render ‘/ajax/script_list’ #HERE!!!
format.html { render action: “new” }

the following is the ‘:partial’ that hopefully updates itself with ajax.

<%= form_for script, :remote => true do |f|%>
<%= f.hidden_field :video_id %>

<%= f.text_field :text, :class=>“xxlarge” %>
<%= f.submit “save”, :class=>“btn” %>
<% end %>

And of course, I need some JavaScript code that implements it.

Could anyone give some tips for that ?