0

"When clicking the link, javascript we take an id from date attribute, and we add it in the form, and we display a modal window."

How to add id value from javascript in the form?

link:

= link_to _('Report this'), '#', class: 'report', data: { comment_id: comment.id,   toggle: 'modal', target: "#report_reasons"}

javascript:

$(function() {
  $(".comments").on("click", ".comment a.report", function(e) {
    e.preventDefault();

    var $this = $(this);
    var commentId = $this.data("comment-id");
  });
});

form:

= simple_form_for(Report.new, url: report_video_comment_path(video.id, "???"), remote: true) do |f|

routes:

resources :videos, except: [:index] do
  resources :comments, only: [:index, :create, :destroy] do
    member do
      post 'report', to: 'reports#create'
    end
  end
end

controller:

class ReportsController < ApplicationController
  before_filter :authenticate_user!

  before_filter :find_comment, only: [:create]

  def create
    @report = @comment.reports.build(report_params)
    @report.user = current_user
    @report.save

    respond_to do |format|
      format.js
    end
  end

  private

  def find_comment
    @comment = Comment.find(params[:id])
  end

  def report_params
    params.require(:report).permit(:type_report, :message)
  end
end

instead of "???", it is necessary to insert data-attr from javascript.

Thanks.

1 Answers1

0

If the form submit path is being altered by the user doing something on the client-side without a page load (or an ajax re-load of just the form content), then yes, you will need to use JS to update the form path. Have a look at these posts:

  1. Rails - change form action based on radio selection
  2. Changing the action of a form with javascript/jquery
Community
  • 1
  • 1
steakchaser
  • 5,198
  • 1
  • 26
  • 34