Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Comments on How to call a function from ruby-on-rails (HTML)?

Post

How to call a function from ruby-on-rails (HTML)?

+1
−4

Let I have a button

<button type="button" onclick = "myfunction()" class="collapsible">Expand content</button>

And, I have a JS function.

function collapse() {
    var coll = document.getElementsByClassName("collapsible");
    var i;

    for (i = 0; i < coll.length; i++) {
        coll[i].addEventListener("click", function() {
            this.classList.toggle("active");
            var content = this.nextElementSibling;
            if (content.style.display === "block") {
                content.style.display = "none";
            } else {
                content.style.display = "block";
            }
        });
    }
}

Usually, we call a JS function in HTMl using onclick. What if it comes to Ruby-on-rails? I noticed that I can't call a function that way in Ruby-on-rails. How to call a function from ruby-on-rails (HTML)?

History
Why does this post require moderator attention?
You might want to add some details to your flag.
Why should this post be closed?

2 comment threads

What exactly do you want to achieve? (I am not asking how you want to achive it) (7 comments)
Same question asked on Stack Overflow. (3 comments)
What exactly do you want to achieve? (I am not asking how you want to achive it)
elgonzo‭ wrote over 2 years ago · edited over 2 years ago

That sounds a lot like a XY problem to me. RoR is a server-side framework. Therefore, ruby code itself is not running on the client, and itself is not able to "reach" into the client browser. Theoretically, what you would have to do is having some JS code in the browser send some fetch request to the server (which you have to define based on your business needs). And based on the response to that request the JS code can then decide whether and which other JS functions should be called.

deleted user wrote over 2 years ago · edited over 2 years ago

elgonzo‭ every code in backend is ruby... I want to call a function of JS from Ruby (HTML).

elgonzo‭ wrote over 2 years ago · edited over 2 years ago

Your comment does not help me understand. Your comment just repeated what you already wrote in your question, so your comment is redundant and does not add any further information/detail necessary for me trying to get a better grasp on what your real goal is that you want to achieve... :-( Is something like this https://github.com/rubyjs/therubyracer what you are looking for?

deleted user wrote over 2 years ago

elgonzo‭ thats written in c++ and ruby. So, easily thats not what i wanted. Actually, i have a function which tells to collapse or expand a div class. Now, i want call that function from HTML. We usually use onclick method to call a JS function. But, when HTML, JS designed in ruby-on-rails the onclick method isn't working anymore. So, i asked is there another way to call that function.

elgonzo‭ wrote over 2 years ago · edited over 2 years ago

So your problem is the onclick handler not working as it should? If so, it has nothing to do with Ruby per se. Look at the HTML document generated by the Ruby-on-Rails backend. Check if the onclick handler has been correctly set up in the generated HTML document. Is the function assigned to the onclick hander declared within the same HTML document? If not, is the js file correctly linked in the HTML document, and can the browser actually successfully download the JS file? (1/2)

elgonzo‭ wrote over 2 years ago · edited over 2 years ago

(2/2) Is the JS code free of errors? Nothing about this is about dealing with Ruby so far. If you diagnose this, you will know how your JS onclick handler fails in the browser. If you know how the JS onclick handler fails in the browser, you will either know how to fix the issue (whether it's in the HTML template/ERB or in the JS code itself), or you will have at least gathered enough useful information to ask a meaningful and answerable question.

deleted user wrote over 2 years ago

elgonzo‭ actually, i have assigned onClick properly. The JS file isn't in the same. It was linked via Ruby. And, the JS function didn’t assigned. When I try to redirect from JS or, HTML. That's function say that not linked to any code ~ something just this. I am not on laptop i can't say it now...