0

So, I have this html.erb and this controller (shown below). What I want is to, if simple_captcha.valid? to increment reports, save, AND close current tab. I want to do it from controller, if possible! (And also, would it be a good practice?)

I saw several examples of this done on view page using javascript, but I know nothing of javascript and, if possible, I'd like to deal with it on controller. But, in case I really have to learn javascript to achieve what I want, which direction should I take?

#view (html.erb)
<h4>To report, complete captcha</h4>
<%= show_simple_captcha %>
<%= button_to "report post",  create_report_post_path(@forum_post.id) %>
-----------------------------------
#controller
def new_report_post
    @forum_post = ForumPost.find(params[:id])
end

def create_report_post
    @forum_post = ForumPost.find(params[:id])
    if simple_captcha_valid?
        @forum_post.reports += 1
        @forum_post.save
        redirect_to ???
        flash[:success] = "Mandou ver."
    else
        redirect_to report_post_path
        flash[:warning] = "Captcha inválido."
    end
end

1 Answers1

1

I don't think, you can close a tab, unless that tab was explicitly opened by javascript. You can refer to this question: link

If however, you are opening the view using javascript. you can send window.close() using a js.erb view.

Instead of redirect_to, it would be something like

    respond_to do |format|
        format.js { render "js_erb_view" }
    end

Inside your js.erb view file,you can send window.close() This would only work if you send an ajax request. One of the possible solutions to make this work :)

Community
  • 1
  • 1
Sudipta Mondal
  • 2,550
  • 1
  • 19
  • 20