Determine which submit button clicked when they have same name - ruby-on-rails

I have a form with multiple submit buttons called "select". I want to be able to tell which was clicked. I can't set the value to the number because I want them to say "select". How can I do this?

You can give them different name attributes instead of different value attributes. For example:
<%= f.submit "select", name: "select_one" %>
<%= f.submit "select", name: "select_two" %>
Clicking the first one will insert
"select_one"=>"select"
into your POST parameters and clicking the second one will insert
"select_two"=>"select"
into your POST parameters.
In your controller, simply use params[:select_one] and/or params[:select_two] as conditions in your logic, like so:
if params[:select_one]
puts "select_one was clicked"
elsif params[:select_two]
puts "select_two was clicked"
end

Related

Ruby On Rails: make radio button default selected first time

I am new to RoR. I want to know how do we make a radio button default selected just first time while creating new record?
I tried
<%= radio_button_tag 'scheduler', 'now', true%> Now <br>
<%= radio_button_tag 'scheduler', 'future'%> Future<br>
It makes the first radio button (having value Now) checked but when i select second radio button (Future) and leave other mandatory fields blank on the form then again the first radio become selected instead of second.
How to resolve this.
Note: the field "scheduler" is just a virtual field on this form that will display another HTML containing a datetimepicker if "Future" option is selected and that datetimepicker field "schedule_date" is the actual field of the model. If option "Now" is posted then simply it will save the current date and time into the database table for that field "schedule_date". So i just have to show/hide an HTML by using these radio buttons.
When you say 'leave the mandatory fields blank' i guess you are submitting the form and getting the edit form back from the server with a warning flash? What I think you need to do here is instead of setting the Now field to true, you need to use a variable and set it to true on when creating a new form. Then when you submit the form to the server the controller's create method should look at the params hash, set the variable, and if the submission fails validation is should return the edit form with the Now variable set to false and the Future variable set to true
so in the create method of the controller you have something like
#now = params[:scheduler][:now]
in the new method of the controller you have:
#now = true
and in the view:
<%= radio_button_tag 'scheduler', 'now', #now%> Now <br>
<%= radio_button_tag 'scheduler', 'future', !#now %> Future<br>
This is true because you have set the first radio button to be selected by default. So when you select the second radio button and then refresh/submit the form, the page comes to its original position losing the previous selected second button.
This is just like when you enter the form fields and then refresh the form or come back again to the form after going back, you loss the data that you filled previously.
Solution:
I say get the checked radio button value and set it using jquery as-
$('input[name="myradios"]').change(function() {
alert($(this).val());
});
Try:
<%= radio_button_tag 'scheduler', 'now', true %> Now <br>
<%= radio_button_tag 'scheduler', 'future', params[:scheduler].eql?("future") %> Future<br>

One select_tag and multiple button_to submit buttons

I have one select_tag and multiple button_to buttons on the same page. I'm looking to use the parameters from the select_tag for several of the buttons, is there a non-form way to do this?
The reason I don't want to use a form is two fold:
1. the number of buttons is dynamic
2. the positioning of the buttons do not follow the form structure (object at the top and submit at the bottom)
-here is one of the create methods that are linked to one of the buttons
def create
params[:options].each do |x|
#connector = Connector.find_or_create_by_options_id_and_follow_id(x.id, current_user.follow(#product).id)
#connector.save
end
end
I checked and this params[:options] is always nil no matter what is selected by me when I test
<%= select_tag :options, options_for_select(#current_user_options.map {|p| [p.name, p.id] }), {:multiple => true} %>
button_to method actually creates a form which has a submit button, with the form action being the url which you had mentioned in button_to. So when you hit the submit button, obviously the options param will be nil, since the button_to form has no data in it
You can use a form itself, and form does not mandate, having an object at the top and submit button below. Submit button just submits the form irrespective of its position in the form
It sounds like you actually need a second field in your form that's a select field or radio buttons to choose your action. Then you have one button that submits the form, and in your controller you can then parse that new action field and decide where to redirect the user.

RoR... my scaffold table doesn't fill when i use radio buttons

I have a problem, i made a scaffold, generating the table "requirements", i want the user to fill the fields of the table in the edit and the new requirement with select boxes and radio buttons. The select box and the radio buttons appear in the explorer but when i select one option or one button, that value selected it's not reflected in the db. The code im using its the next: (As you can see i used the original cicle f.label(:notif_card) and f.text_field(:notif_card) generated by the scaffold, but i deleted the last one and used the select box in this case.)
<%= f.label :notif_card %><br />
<% value = { "Good" => 0, "In Progress" => 1, "Denied" => 2 }%>
<%= select( #requirement, :notif_card , value) %>
<% if value == 1 %>
<% #requirement.notif_card = 1 %>
<%end%>
I just want to delete that text_field and replace it with a select box! Everything you can do i will appreciate it alot! If something needs to be in the model or in the controller besides the code that i'm using please let me know. Thanks for your help!
if you want to use the radio buttons for storing values from form to DB you can use the radio buttons..
the syntax will be like
<b>notif_card</b><br/>
<%= f.radio_button:notif_card,'0'%>Good<br\>
<%= f.radio_button:notif_card,'1'%>In progress<br\>
<%= f.radio_button:notif_card,'2'%>Denied<br\>
<%= f.submit %>
this will store values on the database by using radio buttons.
if you want to use check box method for the same, you can use but it is of Boolean method.so needs to be declared as boolean when start generating scaffolding it.

Rails: Multi-submit buttons in one Form

Say I have an Article model, and in the article 'new' view I have two buttons, "Publish" and "Save Draft".
My question is how can I know which button is clicked in the controller.
I already have a solution but I think there must be a better way.
What I currently used in the view is:
<div class="actions">
<%= f.submit "Publish" %>
<%= f.submit "Save Draft", :name => "commit" %>
</div>
So in the controller, I can use the params[:commit] string to handle that action.
def create
#article = Article.new(params[:article])
if params[:commit] == "Publish"
#article.status = 'publish'
// detail omitted
end
#article.save
end
But I think using the view related string is not good. Could you tell me another way to accomplish this?
UPDATE: Since these buttons are in the same form, they're all going to the 'create' action, and that's OK for me. What I want is to handle that within the create action, such as give the Article model a 'status' column and holds 'public' or 'draft'.
This was covered in Railscast episode 38. Using the params hash to detect which button was clicked is the correct approach:
View:
<%= submit_tag 'Create' %>
<%= submit_tag 'Create and Add Another', name: 'create_and_add' %>
Controller:
if params[:create_and_add]
# Redirect to new form, for example.
else
# Redirect to show the newly created record, for example.
end
it can also be done on the form_for helper like this
<%= f.submit "Publish",name: "publish", class: "tiny button radius success" %>
<%= f.submit 'Mark as Draft', name: "draft", class: "tiny button radius " %>
and the logic is the same on the controller
if params[:publish]
// your code
elsif params[:draft]
// your code
end
We solved using advanced constraints in rails.
The idea is to have the same path (and hence the same named route & action) but with constraints routing to different actions.
resources :plan do
post :save, constraints: CommitParamRouting.new("Propose"), action: :propose
post :save, constraints: CommitParamRouting.new("Finalize"), action: :finalize
end
CommitParamRouting is a simple class that has a method matches? which returns true if the commit param matches the given instance attr. value.
This available as a gem commit_param_matching.
I remember coming across this problem once. You cannot keep two buttons and then call some action based on the params[:commit]. the submit button onclick is going to call the url the form refers to. There are certain bad ways to get the desired behavior. Keep a button to call the action the form refers to and to get another button to call a action, I used a link_to and then changed the styles to match a button. Also, alternatively you can use jQuery to change the url the form would call, hence deciding what action is invoked at run-time. Hope this helps.
You could also set some data attributes on the submit buttons and use JavaScript to change out the form action on click of one of the buttons
usually i using the suggestion given by John Topley (see answer above).
another way is using JQuery /JS changing the form action attribute- upon clicking the submit button
example:
form_tag({} ,:method => 'post', :id => 'reports_action') do
.......
.......
submit_tag 'submit', :onclick => "return changeAction();"
end
and then .....
function changeAction(){
$('#reports_action').attr('action','my_new_action');
}

Rails - Using checkboxes to select multiple objects and having a choice of actions to perform

Id like to be able to create checkboxes for a list of objects. Then offer the user a number of actions to perform on the objects selected. I.e. delete, archive etc.
I know of ryan's screencasts but it doesnt explain how to create links to multiple actions for the selected objects. It just showed him create a form_tag with a url to one action and a submit button.
I think you can do it in two ways.
First: you can add as many buttons to one form as you want:
<%= f.submit "Action 1" %>
<%= f.submit "Action 2" %>
<%= f.submit "Action 3" %>
And all of them are submitted to one action in which you can check:
if params[:commit] == "Action 1"
do some stuff for action 1
elsif params[:commit] == "Action 2"
do other stuff
... and so on
end
Another way is to use some js. On example when user clicks on button "Action 2" it changes "action" parameter in form and submits data to this action.
EDIT:
In case of translated websites, you can do it like this:
<%= f.submit (I18n.t :action_1) %>
and in controller:
if params[:commit] == I18n.t :action_1
...
end
And in en.yml add:
action_1: Action 1
In pl.yml add:
action_1: Akcja 1
and so on.
You can always change the name of the submit button. Just look for the params[:button_name] instead of params[:commit].
You'll need some sort of method in the controller to handle the ability to update_many objects. Maybe a before filter to dispatch...

Resources