Reputation: 840
I want to change the value of a Ruby local parameter in my .erb file when a user clicks on a bit of text. (I am using Ruby with Sinatra, not Rails). The code looks like this:
<div class="modal-body">
<!-- Get ERB to work here -->
<p><i>No account? <a href="#" onlick="<% new_user = !new_user %>">Register here</a></i></p> <!-- Should change new_user to true if clicked -->
<form class="input-group" style="margin: 0 auto" method="POST" action="/nanotwitter/v1.0/users/session">
<input class="form-control" type="text" name="username" placeholder="Username">
<br><br>
<input class="form-control" type="password" name="password" placeholder="Password">
<% if new_user %>
<br><br>
<input class="form-control" type="password" name="password2" placeholder="Repeat password">
<% end %>
<br><br>
<div class="input-group" style="margin: 0 auto">
<button type="button" class="btn btn-default form-control" style="width: 50%" data-dismiss="modal">Close</button>
<button class="btn btn-primary form-control" style="width: 50%" type="submit">
<% if new_user %>
Register
<% else %>
Log In
<% end %>
</button>
</div>
</form>
</div>
So obviously the Ruby code in the onlick attribute evaluates immediately, and I want it to only execute when the user clicks. I've searched this site and the internet, and I'm still not sure what the correct/standard implementation for this would be.
Upvotes: 0
Views: 705
Reputation: 327
First, ruby dont interact with user events. Javascript does.
Second, what is new_user
? Simple variable? You want to use it to, say, swap forms for sign up\ sign in ? Or you want user to really register through this method?
If the case is to show sign in\ sign up form to user based oh his choice (new\registered), you should do it with only javascript.
In general, you should prepare markup for two different forms (register\login) and hide them according to some javascript variable, which user can choose.
Upvotes: 1