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?
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