0

In my Rails app, I let users cycle through different questions. I pass the questions to my view as well as an array containing indexes for all questions via my controller:

def index
    @questions = Question.unanswered
    @question_array = Array.new 
    @questions.each_with_index do |question, index|
      @question_array << index
    end
end

Then in my view, I use javascript to let users cycle through questions when clicking on an icon:

    <div class="questions homeSection">
        <div class="titleContainer questionTitle">
            <p class="updateTitle">Recent Questions <icon class="fa fa-refresh"></icon></p>
        </div>

        <div class="question">
            <% question_index = @question_array.sample %>
            <% question = @questions[question_index] %>
            <div class="question_content"><%= question.description %></div>
            <div class="question_byline">from <%= link_to question.step.project.title, project_steps_path(question.step.project, :step_id => question.step.id) %> </div>
        </div>
    </div>

<script type="text/javascript">

    // pick a random question to display
    function cycleQuestion(){
        <% old_question_index = question_index %>
        console.log("old_question_index: <%= old_question_index %>");
        <% new_question_array = @question_array - [old_question_index] %>
        console.log("new_question_array: <%=new_question_array%>");
        <% question_index = new_question_array.sample %>
        console.log("new question_index: <%=question_index%>");
        <% question_description = @questions[question_index].description %>
        console.log("new question: <%=question_description%>");

        $('.question_content').hide().html('<%=question_description%>').fadeIn();
        $('.question_byline').hide().html('from <%=link_to question.step.project.title, project_steps_path(question.step.project, :step_id => question.step.id) %>').fadeIn();
    }
</script>

The issue I'm having is that the ruby variable "question_index" does not seem to be persisting so that the refresh works once but then doesn't work again. You can see this in my console output:

clicked refresh 
old_question_index: 1 
new_question_array: [0]
new question_index: 0 
new question: How do I ask a question? 

clicked refresh 
old_question_index: 1 
new_question_array: [0] 
new question_index: 0 
new question: How do I ask a question? 

How can I update the ruby variable "question_index" every time cycleQuestion is called?

1
  • 2
    You can't have server side functionality on the client!!!! If you want to do something with javascript you must do it through ajax which will access some controller action in you deployment. Commented Mar 12, 2014 at 16:32

1 Answer 1

1

You need to understand how rails (and any website) works:

  • the client (browser) sends a request to the server
  • the server generates a page of html (and/or javascript, xml, or anything else) using ruby. There is no ruby code in the result.
  • the result (the response) goes back to the browser, which might then run more javascript.

The client knows nothing about how the page was generated.

If you want some server-side data to be usable by javascript in the client, you will need to add it into the response text, and then make the javascript look for it in that text: in this way you pass data to the client. But, the client never has access to the server side data.

Sign up to request clarification or add additional context in comments.

1 Comment

You're welcome. This is all really fundamental stuff - i'd strongly recommend you read up a bit more on the basic architecture of Rails (and the web in general) before doing much coding: the code will make a lot more sense! One of the problems with learning rails is that there is so much "magic" you run the risk of not knowing what is actually happening at all.

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.