1
votes

Here is a create action:

def create
  @folder = Folder.new(params[:folder])
  @folder.user_id = current_user.id
  if @folder.name != "Folder Name" && @folder.save
      respond_to do |format|
          format.html { redirect_to messages_url(:target => params[:target], :search => params[:search]) }
          format.js
      end
  else
      flash[:notice] = "Error saving folder. Please try again."
      redirect_to messages_url(:target => params[:target], :search => params[:search])
  end
end

What I'm trying to accomplish is this: when the save is successful, return create.js.erb (this works).

When the save is not successful, redirect_to messages_url normally (i.e. with HTML).

When the latter happens, the redirect is happening using JS, then I get an error showing that the index action (of course) cannot find a JS template. How can I do this redirect in HTML?

1

1 Answers

4
votes

you need to response to format too when save is not successful and can use js helpers to redirect the page. The code could be like this

if @folder.name != "Folder Name" && @folder.save
      respond_to do |format|
          format.html { redirect_to messages_url(:target => params[:target], :search => params[:search]) }
          format.js
      end
  else
      flash[:notice] = "Error saving folder. Please try again."
      respond_to do |format|
        format.html { redirect_to messages_url(:target => params[:target], :search => params[:search])}
        format.js {render(:update) { |page| page.redirect_to messages_url(:target => params[:target], :search => params[:search])}}
      end
  end