I've got a flashcard system that allows users to create and go through sets of flashcards. I'm currently refactoring it to make it more efficient, but I'm running into a similar problem that I was before. Rendering the buttons through Javscript causes the buttons to not be clickable anymore unless the Javascript is reloaded. Instead of having 500 Javascript pages running in the background, I changed it so I didn't need it anymore. However, my paths are passing the parameters of the current card to the controller. Since I'm no longer generating the buttons via JS, however, the buttons are passing the parameter as the original card at page load.
<%= link_to ">", flashcard_path(type: "next", old: @card.id, status: @is_read), remote: true, class: "btn" %>
How can I update the @card.id without re-rendering the entire button? I'm open to suggestions if I need to change a bunch of stuff, too. I'm sure I m doing this in a strange way- as that is how I tend to do things. :)
This is the javascript that re-renders the flashcard and the accompanying controller method.
$('#id_number').html("<%= @card_num %>");
$('#flashcard-title').html("<%= escape_javascript(@card.title) %>");
$('#flashcard-lines').html("<%= escape_javascript(render partial: 'flashcard_lines', :locals => { card: @card, is_read: @read }) %>");
$('#flashcard-body').html("<%= escape_javascript(@card.body) %>");
def flashcard
all = FlashCard.all
old_flashcard = FlashCard.find(params[:old].to_i)
old_index = FlashCard.all.index(old_flashcard)
case params[:type]
when "new"
@card = FlashCard.new
@card.save
@read = false
when "edit"
@card = old_flashcard
@read = false
when "next"
if old_index == all.length - 1
back = 0
else
back = old_index + 1
end
binding.pry
@card = all[back]
@read = true
when "back"
if old_index == 0
back = all.length - 1
else
back = old_index - 1
end
@card = all[back]
@read = true
when "save"
old_flashcard.save
@card = old_flashcard
@read = true
when "delete"
old_flashcard.destroy
@card = FlashCard.all.last
else
@card = FlashCard.find(0)
@read = true
end
if params[:status]
@read = params[:status]
end
@card_num = FlashCard.all.index(@card) + 1
respond_to do |format|
format.html
format.js
end
end