Reputation: 15
Hi I have the following problem. I would like to call a controller method which executes some sql statements by onklick option of a button or a link.
So I got a method "publish" in my controller. It looks sth like this:
def publish
do execute sql statements
flash[:notice] = 'sql statements executed.'
end
And in my view I would like to have a button or a link to call this method like this:
<input type="button" onclick="<% controller.publish %>">
But I get the error:
undefined local variable or method
I also tried to route it to use this method as :action. That did not work as well. I searched now for a while and found some solutions by making this method a helper_method, though I get the same error again. I'm pretty new to Ruby and Rails so I hope you can help me.
Upvotes: 1
Views: 10799
Reputation: 755
@alex-d's answer seems to be enough. Just use button_to
and at the end of your publish
action you can redirect to the same page (or the same action as before, if you're loading records to show in the view).
EDIT: From @alex-d's answer:
Create a route which will invoke publish when requested. For example, say the route is /publish.
GET '/publish' to: 'Controller#publish'
In your view, put something like this:
<input type="button" onclick="window.location = '/publish'">
Now on your controller method, redirect:
def publish
do execute sql statements
flash[:notice] = 'sql statements executed.'
redirect_to route_where_the_button_view_is_path #(or render if that controller method isnt necessary)
return #to avoid multiple render or redirect error (maybe not necessary)
end
PD: My code may not work at the first try, I'm working this out from memory.
GL & HF.
Upvotes: 0
Reputation: 4255
First, if your SQL statements are going to change any data (which it looks like they are meant to), it's important that you use a POST request and not a GET request. To do that, you either need to use a basic form submission instead of clicking on a link, OR use javascript to submit the POST request when clicking on the link. Let's talk about the second way since that will stay consistent with how you want the app to look.
Assuming jQuery, you can use the jQuery.post method to post to your publish
action when the button is clicked. In the success callback, you can display the message about the sql statements having been executed.
Upvotes: 1
Reputation: 25
Your ruby on rails code is server side.
The code in your view e.g <input type="button" onclick="<% controller.publish %>">
is client side.
You can't call server side methods directly from client side code, since the client code is just running in the user's browser. I've not used RoR so don't know the details but you will have to submit a message to the server from the client side code, at the server side you will then want to receive this and call the publish method from there. Hope this helps.
Upvotes: 1
Reputation: 30445
If, in your view, you try something like <% controller.publish %>
, that will call the publish
action when the view is rendered, not when a user clicks on the button.
You can do something like this:
Create a route which will invoke publish
when requested. For example, say the route is /publish
.
In your view, put something like this:
<input type="button" onclick="window.location = '/publish'">
The value of the onclick
attribute must be valid JavaScript, which will be executed when the button is clicked. In JS, setting window.location
causes the browser to navigate to a different page. In this case, we are making the browser navigate to /publish
, which will cause your publish
action to be invoked. Whatever you render in publish
will then appear in the browser.
In the code for publish
which you show above, your Ruby syntax is wrong, but I am assuming that it is just a sample, not the actual code which you are using.
Upvotes: 1