gazubi
gazubi

Reputation: 561

Rendered form does not have controller variables

controller action

def board
   @ids = params[:ids] if params[:ids]
   respond_to do |format|
      format.js do
        render :edit_modal
      end
   end
end

edit_modal.js.erb

$("#editModal .js-modal-body").html(
  "<%= j render "edit_board.html.erb" %>"
);

$('#editModal').modal('show');

edit_board.html.erb

<%= simple_form_for :order_fulfillment,
 url: update_multiple_path,
 method: :patch,
 remote: true do |f| %>
  <%= f.hidden_field :edit_ids, value: @ids %>
<% end %>

For some weird reason, my form does not have any value inside the hidden fields. However, when I do a binding.pry like this:

def board
   @ids = params[:ids] if params[:ids]
   binding.pry
   respond_to do |format|
      format.js do
        render :edit_modal
      end
   end
end

The form seems to have the value of @ids populated. binding.pry halts the program at that line of execution. I am unable to understand why this works with binding.pry? Is it because @ids does not have any values when edit_modal.js.erb is rendered ? if that is the case why, is it so, and how can I make @ids to be populated inside my edit_board.html.erb.

Upvotes: 0

Views: 41

Answers (2)

akbarbin
akbarbin

Reputation: 5105

Try to change several codes here

Controller

To use ajax in rails, you only add format.js

def board
   @ids = params[:ids] if params[:ids]
   respond_to do |format|
      format.js
   end
end

Then, rename edit_modal.js.erb into board.js.erb because you have to use file same as your method. Inside of your board.js.erb you have to change into

$("#editModal .js-modal-body").html(
  "<%= j render "edit_board" %>"
);

$('#editModal').modal('show');
$('#order_edit_ids').value("<%= @ids %>");

Finally. Rename edit_board.html.erb to use underscore _edit_board.html.erb

I hope this help you.

Upvotes: 0

Deepak Mahakale
Deepak Mahakale

Reputation: 23661

Rename .js file to action name you don't need to render them explicitly

def board
   @ids = params[:ids] if params[:ids]
   binding.pry
   respond_to do |format|
      format.js
   end
end

rename edit_modal.js.erb => board.js.erb

it will work

Upvotes: 1

Related Questions