0

I define

 @variable1= '1'

in controller. In view, i call <%=@variable1%> and it show 1. Now I click button to call

def change_variable
@variable1= '2'
end

what do I do to see '2' in view?

5
  • What are you rendering in the view associated with change_variable? Commented Nov 26, 2013 at 7:35
  • I'm new to ruby on rails,please tell me how can I just change value of @variable1 and don't do anything else Commented Nov 26, 2013 at 7:39
  • 2
    You cannot just change the value of @variable1, you can make an AJAX call or just create a new route and new view for change_variable action. Commented Nov 26, 2013 at 7:40
  • How can I update value of @variable1 and show change in same view ? I really need it. Commented Nov 26, 2013 at 7:45
  • 1
    AJAX is what you're looking for, you can read more about it in the documentation Working with JavaScript in Rails Commented Nov 26, 2013 at 7:47

1 Answer 1

1

The ruby code you have posted only runs at the time the page is being generated.

Once it is sent to the browser it becomes a static page.

In order to run the change_variable method you are referring to an http request of some description needs to be made (e.g. submit button, XMLHttpRequest etc.)

If you want the variables to change on the actual page, you will need to look into some javascript solutions.

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

2 Comments

Thanks, can you give me an example?I have tried this for a day and got nothing.
The amount of information and concepts required to get what you are talking about is unfortunately too much to be able to do on StackOverflow. Perhaps you could start with some videos on Railscasts, such as this one railscasts.com/episodes/205-unobtrusive-javascript

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.