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 6993677
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:47:41+00:00 2026-05-27T19:47:41+00:00

I want to have a button that, when clicked, will execute rails code. Is

  • 0

I want to have a button that, when clicked, will execute rails code. Is javascript necessary for this? Is there some ruby-javascript (.js.rb)?

if button is clicked… execute A
However i do not want to redirect or go to a new view, just ruby code to be executed.

  • 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-27T19:47:42+00:00Added an answer on May 27, 2026 at 7:47 pm

    Yes. Try googling unobtrusive javascript. With rails specifically the following is the general principle (rails v 3.0 or greater), and is most often used for forms.

    Here is an example of using a remote form to post a “message” via javascript.

    First setup your controller to respond to javascript. So for example a “create” action would look something like this:

    def create
      @message = Message.new(params[:contestant])
      #do whatever you want
      respond_to do |format|
      if @contestant.save
         format.js
         format.html { redirect_to(@message, :notice => 'Message was successfully created.') }
      else
         #deal with errors here/ redirect wherever you want
      end
    

    Now create a file called “create.js.erb”. This follows the rails naming conventions for controller actions, but note the js.erb extension.

    In this code you can put whatever javascript you want to respond with, and in rails 3.1 or you can use jquery by default as well.

    $(#messageform).html("<h3>Thanks for sending your message we will be in touch shortly</h3>");
    

    Then in your view that you want to initiate this javascript call you would put a form like this:

    <div id="messageform">
      <%= form_for @message, :remote => true do |f| %>
        <h2>Send us a message</h2>
          <p>
            <%= f.label :email %>
            <%= f.text_field :email %>
          </p>
          <p>
            <%= f.label :message %>
            <%= f.text_area :message %>
          </p>
          <p>
            <%= f.submit "Send message!"%>
          </p>
       <% end %>
    </div>
    

    This is a standard form_for. Only thing special here to notice is the :remote => true parameter at the beginning.

    Anyways hope this helps 🙂

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

Sidebar

Related Questions

I have a button that will execute a stored procedure. After it's been clicked,
In my Delphi Project i want to have a 'Settings' button that when clicked,
Ok so i have this Jquery that will toggle some radio buttons on the
Alright, so I have a button that when clicked, will change the class of
I want to have a button that has numbers in the range 0 ...
Suppose, I want to have a link or a button that when user click
I have a Login button that I want to be disabled until 3 text
I want to have a button on my toolbar that has a little down
I have a datagrid that I want to add a button/s to at runtime.
I have an array of strings that I want to use for button titles

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.