1

Simply scenario -- I have two buttons both of which call the same controller/action. The controller action sets a random number, @random_number.

If I click button 1, I want the view to change (via JS) by displaying the @random_number in RED. If I click button 2, I want the view to change (via JS) by displaying the @random_number in GREEN.

What is the DRY-est way to do this? My understanding of rails's controller/view layers is that each controller/action is associated with a corresponding view. But in this case, I want to use the same business logic (generate a random number) for both button clicks but the resulting view depends on which button is clicked.

One option is to pass the button id to the controller/action and then use the button id in the controller/action's associated RJS file to execute the appropriate javascript. But my gut tells me that this is not the best way. Is there a better way?

A quick code sample would be much appreciated.

Thanks.

1
  • you should accept answers to your questions if you find them helpful to get more answers. Commented Jan 9, 2012 at 9:25

2 Answers 2

3

You should have the same view and same controller method generate a random number. However, at the front end you should use javascript or jquery to set the color property of the test. To start with such an approach you could look into ajaxSuccess and click event binders in jquery.

Another approach, if you want to avoid using javascript, would be to set a data value in your link field(HTML5). The data value should be sent to the controller as part of the request that is generated. This should simply be set when you're displaying the text dynamically. This way you won't be repeating any part of the code in the view or controller.

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

3 Comments

Yea I was hoping to do something similar to what you describe in the first paragraph. But it raises two questions: 1) what should the response be for the controller/action which sets @random_number? 2) how can I get access to @random_number in the ajaxSuccess callback?
1) The response could be one of two things: it could either be just the number, or it could be the javascript that does the manipulation at the client side. 2) You can read about how to get the response in ajax here: api.jquery.com/ajaxComplete
Guys I have a similar question here, that I can not solve based on this answer. Could you take a look at it? stackoverflow.com/questions/34817690/…
2

If it is just to show the value if RED or GREEN, then why not handle it in the front-end(if it is not something to be made secure, then you could avoid a server-sided template). If you want to handle this on the server(to make it secure), then you could call two different actions for the Button-1 and Button-2 and move the code which generates a random number to a private function which you can call from the actions. If any specific values have to be passed to generate the random number based on the specific button that was clicked then you could pass the values to the private function and handle it there.

As for a code example I would handle it this way.

def action_for_button_1
  @random_number = generate_random_number(args)
  # render template to show in green
end

def action_for_button_2
  @random_number = generate_random_number(args)
  #render template to show in red
end


private

def generate_random_number(args)
  # generate the random number and return it.
end

1 Comment

Yeah this would work. Assuming I have to call the server, is there a way to do it with one action? Assuming it is an AJAX request originating from /index.html, can I code the view update logic in index.html itself in the form of two callbacks, one for each button? This is as opposed to coding the callback logic in action_for_button_1.rjs.erb and action_for_button_2_rjs.erb. Is this possible?

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.