Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7043653
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T02:18:58+00:00 2026-05-28T02:18:58+00:00

Simply scenario — I have two buttons both of which call the same controller/action.

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T02:18:59+00:00Added an answer on May 28, 2026 at 2:18 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My scenario is this - I have two ASP.net websites. Both sites run on
I have a simple scenario, where two servers are connected through a gigabit link.
I have a very simple scenario, using NHibernate: one abstract base class animal; two
Ok, i have simple scenario: have two pages: login and welcome pages. im using
Scenario: I have an application (C#) that expects a SQL database and login, which
Here's my scenario: Let's say I have a stored procedure in which I need
A simplified scenario: I have a List<Foo> . Foo has two properties Description (
Here is the scenario: In my working directory, I have a number of files
I have the following scenario which I've been using to return a single row
Scenario: I have a WPF desktop application which will be distributed on different machines

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.