How can I remove the HTML from showing in collection_select
such as the <\/option>\n
, <\/select>
, <\/div>');
This is the HTML output before javascript kicks in:
<select name="duel[duelers_attributes][1][challenge_id]" id="duel_duelers_attributes_1_challenge_id"><option value="29">Climb Mount Everst by May 20, 2017</option>
<option value="24">Run by Feb 20, 2017</option>
<option value="26">See Drive-thru Movie by Feb 20, 2017</option>
<option value="28">Run 5 Miles on Weekdays for 10 Days Starting Jan 20, 17</option>
<option value="25">Bungee Jump by Feb 20, 2017</option>
<option value="27">Journal on Weekdays for 10 Days Starting Jan 20, 17</option></select>
This is the HTML output after javascript kicks in:
<select name="duel[duelers_attributes][1][challenge_id]"
id="duel_duelers_attributes_1_challenge_id">
$("#dropdown-no-2").html(
"\n <option value="\"\""><\/option>\n</option>
<option value="\"24\"">Run by Feb 20, 2017<\/option>\n</option>
<option value="\"25\"">Bungee Jump by Feb 20, 2017<\/option>\n</option>
<option value="\"26\"">See Drive-thru Movie by Feb 20, 2017<\/option>\n</option>
<option value="\"27\"">Journal on Weekdays for 10 Days Starting Jan 20, 17<\/option>\n</option>
<option value="\"28\"">Run 5 Miles on Weekdays for 10 Days Starting Jan 20, 17<\/option>\n</option>
<option value="\"29\"">Climb Mount Everst by May 20, 2017<\/option><\/select>\n<\/div>"
)</option></select>
Somehow things seem to get progressively worse with more options??
_dueler_fields.html.erb
<%= f.select :user_id, options_for_select(@challengers.collect { |challenger| [challenger.id] }) %>
<%= f.select :challenge_id, options_for_select(@challenger_challenges.collect { |challenged| [challenged.full_challenge, challenged.id] }) %> ,
# The problem occurs only AFTER the javascript kicks in and replaces the above line with collection_select(:dueler...
<script>
$('#duel_duelers_attributes_1_user_id').change(function () {
var user_id = $(this).find(":selected").val();
var address = "<%= user_challenges_path %>/".concat(user_id);
$.get(address, function(data) {
$("#duel_duelers_attributes_1_challenge_id").html(data);
});
});
</script>
routes
get 'duels/user_challenges/:id', :to => 'duels#user_challenges', as: 'user_challenges'
get 'duels/user_challenges/:id/:user_id', :to => 'duels#user_challenges', as: 'select'
user_challenges.js.erb
$('#dropdown-no-2').html('<%= j render partial: "user_challenges" %>')
_user_challenges.html.erb
<%= collection_select(:dueler, :challenge_id, @challenges, :id, :full_challenge, include_blank: true, id: 'dropdown-no-2') %>
AJAX console output
$('#dropdown-no-2').html('<select name=\"dueler[challenge_id]\" id=\"dueler_challenge_id\"><option value=\"\"><\/option>\n<option value=\"24\">Run by Feb 20, 2017<\/option>\n<option value=\"25\">Bungee Jump by Feb 20, 2017<\/option>\n<option value=\"26\">See Drive-thru Movie by Feb 20, 2017<\/option>\n<option value=\"27\">Journal on Weekdays for 10 Days Starting Jan 20, 17<\/option>\n<option value=\"28\">Run 5 Miles on Weekdays for 10 Days Starting Jan 20, 17<\/option>\n<option value=\"29\">Climb Mount Everst by May 20, 2017<\/option><\/select>')
ADDITIONAL ELABORATION
The larger goal is that upon the current_user
selecting a :user_id
that the selected user's challenges should appear in the collection_select
.
challenges_controller
def show
@challengers = User.where(id: current_user.following_ids)
if (params[:challenger_id]).present?
@challenger = User.find(params[:challenger_id])
else
@challenger = User.find(10)
end
@challenger_challenges = @challenger.challenges
@duel = Duel.new
@duel.duelers << Dueler.new
end
duels_controller
def user_challenges
@user = User.find(params[:user_id])
@challenges = @user.challenges.order(:created_at)
end
def new
@duel = Duel.new
end
duelers_controller
def new
@dueler = dueler.new
end
duel.rb
class Duel < ActiveRecord::Base
belongs_to :user
belongs_to :challenge
has_many :duelers
accepts_nested_attributes_for :duelers, :reject_if => :all_blank, :allow_destroy => true #correct
end