1

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 Answer 1

4

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
Sign up to request clarification or add additional context in comments.

Comments

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.