save but don' redirect,

Rails 3.1.3

I have Video, Script classes having

Video 1----0..n Script

association.

in "Video" show.html.erb, there are both

  <%= render :partial => "new_script",       :locals => { :script => Script.new(:video_id => @video.id)} %>

and

    <%= 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 = Script.new     respond_to do |format|       format.html       format.json { render json: @script }     end   end

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 }     end

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.

soichi

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

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 page.

So I tried in script controller,

  def create     @script = Script.new(params[:script])     respond_to do |format|       if @script.save         format.html { notice: 'Script was successfully added.' }         format.js do           render '/ajax/script_list' #HERE!!!!!         end       else         format.html { render action: "new" }       end     end

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 ?

soichi