0

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
2
  • Can you post your javascript? Commented Dec 6, 2014 at 17:33
  • There isn't much relevant Javascript since I took most of it out, but I'll edit the post with the Javascript that is run as the response from the controller. Commented Dec 6, 2014 at 17:44

1 Answer 1

1

I am not sure if I am understanding your problem correctly, but maybe you could do something like this?

$('#current_btn_id').attr('old',new_id);
Sign up to request clarification or add additional context in comments.

3 Comments

I don't think that will quite work- I'm not trying to change the HTML. I'm trying to update the link_to path. the link_to url is flash_card_path(old: @card). I need it to be that same path, but I need to update @card to be a different value. The attr() selector doesn't change the rails generated bath, does it?
Oh I see! Sorry didn't notice its was inside the path helper.Can you make it in a way that you just compose the link without the helper and change via javascript? ex. flashcards/next/<%=oldcard.id%> and replace it with the new one?
I thought about doing it that way. I actually originally wrote it through html itself then made it all dynamic by converting it into erb. I'll try that one by adding on an id to the route and then using js to manually change the href. Thanks for the idea!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.