I have two buttons in my page.html.erb file that updates the values in a table.
<div class="container container-1">
<button class="_button _button-2">action1</button>
<button class="_button _button-3">action2</button>
</div>
When I press a button I want it to execute a piece of code in the pagecontroller and display the result in the table. I am able to do all these but struck at recognizing button. How do I execute ruby code on click of the buttons? I want to do it with rails not JavaScript.
You should define a an AJAX request for that method, because Ruby-Code is serverside and The button-click is clientside.
I allready asked that in an earlier question:
How to call a Method from controller in rails?
Related
I am trying to figure out how can I use the button value from view to corresponding controller. I have three button which I have grouped them. Based on user selection, I want to do different functions (eg: List the task, form to add the tasks..etc). For that I want to pass the button value to the controller.
<div class="btn-group btn-group-justified">
Task Lists
Task Category
Add Task
</div>
I am not sure if there is better way to do this than button groups. I am not familiar with javascript also.
To make it clear, I have user information on the left side of the page which is already designed. These buttons are on the top of the right side of the page. Upon selecting the buttons, right side of the page should change/reloaded based on the selection.
You can try following
<div class="btn-group btn-group-justified">
Task Lists
Task Category
Add Task
</div>
You can use links to go to the controller action you want (if I understand your question correctly). You would use the paths/routes to the specific action you want. It would look something like this:
Task Lists
Or you can do it in rails direction, using the link_to method:
<%= link_to "Task Lists", tasks_lists_path, class: "btn btn-primary" %>
Again, this depends what the routes you are using are. If you want to pass parameters with the link, it gets a bit more complicated, but not much.
For more information: http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to
One option is triggering an ajax post request when the button is clicked and send the data to a particular controller action.
Could also just pass params through a link_to.
I need an help on How to submit 2 form's POST methods both
with a one single click.
Suppose that I have two seperate forms
I want my secound form to be submited at the time I click on the first form action button.
You need to do this with Javascript
first place a button that is going to submit both forms like this in your html
<button type="submit" class='input_submit' style="margin-right: 15px;" onClick="submitBothForms()">submit both</button>
then your submitBothForm method has to includes these lines
document.forms["first_form_name"].submit();
document.forms["secound_form_name"].submit();
In my rails form i want to get mobile/contact details of a user. It could be more than one. How can I add text boxes dynamically once the user clicks the "add more" button.
To do this you will need to do some javascript and some ruby code to get working .
First on click of Add More,you need to show a partial already hidden inside your view.If This needs to be dynamic,then you need to call an ajax and then load the partial in a predefined placeholder
for example:-
load partial in advance in our view and hide it first
<div id="user_contact_details" style="display:hidden;">
<%= render(:partial => 'user/contact_details') %>
</div>
######Now,ON CLICK of the Add More button,show this hidden partial using js
##add more button
Add More
$('#add_more').click(function(){
##toggle partial placeholder
$("#user_contact_details").toggle('slow');
})
...the same can be done using ajax,you can also load partial using some conditions such as if user has more than one contact details..then load another partial with different form than previous one...
Although..this might not be the unique solution,but atleast you can have an idea of how it can done.
I did this for you in JSFIDDLE, please refer this from my JSFIDDLE.
http://jsfiddle.net/Manoj_Menon/eyxt6kjh/
Is there any possibility of having a button which will execute and action in a controller without a form ?. Also, i would like do do something like this, but i see that's not possible:
<g:form action="addFavourite">
<td>
<g:submitButton name="${it.area.name}" value="Add" class="button small blue"/><br><br>
</td>
</g:form>
To name the button with a value that comes from a controller isnt working. Any possible alternative for that? It gives me a null-error-code. And i'm 100% sure the value isnt null..
You can create a button outside a form that executes a controller action when it's clicked using the remoteFunction tag
<button type="button" name="myButton"
onclick="${remoteFunction(action:'bookByName', controller: 'book'
params:'\'bookName=\' + this.value')}">Click Here</button>
It kind of depends. If you want a button to submit to a server via a standard POST then no. HTML doesn't even have a button that works without a form. You can fake this with an image link that looks like a button, but really it just submits via a standard anchor tag. And this would perform a GET, not a POST.
However, if you want to use ajax, you could skip the Grails tags (as I often do) and use the HTML BUTTON element. Could even use the remoteFunction to make the ajax call if you want.
UPDATE: Doh! 2 of the same answers. :)
What does "it" stands here for? I think that is the culprit..
<g:submitButton name="${it.area.name}" value="Add" class="button small blue"/>
I'm using same partial view in create.aspx view page twice, part of my code is:
<%Html.RenderPartial("LocationVerifyControl"); %>
<%Html.RenderPartial("LocationVerifyControl"); %>
In the mvc view user controls, it had a button and test code for it:
$('#btnVerify').click(function() { alert("clicked by btnVerify");}
I watched the warning dialog pop up twice when clicked the button, the main view page had contents with same ids. How to solve this kind of problems? To prevent same ids in main view page, put my using html ids in the view data and pass them to partial view?
If I understand your question correctly, you are looking to add unique functionality to two different buttons with the same id.
It's not really valid to have more than one element with the same id. If there will be multiple you should just make it a class. You might want to first run your markup through W3C Validation.
Second you can do this by wrapping the partials in a div or whatever element you like. Something like this:
<div id="first">
<% Html.RenderPartial("LocationVerifyControl"); %>
</div>
<div id="second">
<% Html.RenderPartial("LocationVerifyControl"); %>
</div>
Then your script section can reference the buttons independently.
$('#first #btnVerify').click(function () {
alert('first button click');
});
$('#second #btnVerify').click(function () {
alert('second button click');
});
Notice that I'm just saying to find the id=first then find a id=btnVerify within the id=first.