update attributes in dynamic form rails - ruby-on-rails

I have dynamic form with conditional attributes.
E.g. if radio button "condition1" is checked, when "price" field hides dynamically via jQuery, and "percentage" field appears.
Let's say I created record with "condition1" radio box and set "percentage" value. Then, I decided to update record, and disabled "condition1", so percentage field hides, and I set new value for "price".
The problem is, that if I disable "condition1", "percentage" parameter is still present, and what I need is either "price" or "percentage" parameter at a time. I can removeAttribute() with jQuery on click, but this is bad user experience, because in some fields I have multiline textareas and I don't want to force users to write all again if they accidentally clicked on radio button.
I tried playing with ActiveModel::Dirty with no luck
in edit action of object's controller
if #object.condition_changed? && #object.condition_was == "condition1"
#object.attribute(:percentage, nil)
end

you can run a after update callback like
after_update: set_nil_to_unrequired_field
def set_nil_to_unrequired_field
update_column('your field ', nil) if "other field is present"
end

Related

Submit form not allowed if tag empty

I have form with ngTagsInput field that I submit on button click.
But if user press space a few times and text value looks like text: " " then I am not allowed to submit my form because formName.$valid is false. Is there any way to allow that to happen with ngTagsInput API? or any other way to accomplish that?
It is doable with allowLeftoverText set to true.

Ruby On Rails f.checkbox hidden field

I have a check box on a rails form like this:
.columns.small-1
= f.label :private
= f.check_box :private
if the box is unchecked, the form sends correctly with one value passed. If the box is checked the form sends the same field twice with one value as 0 and one as 1, which is causing the box to always show as unchecked" and have a value of 0. Been researching hidden fields in rails but not sure how to apply to this problem.
This is a normal behavior. By default, f.check_box renders a hidden field with value 0. Right after that hidden field, it renders an input field of type "checkbox" with the value 1. The input field and hidden fields have the same name. When both of these values are sent to the server, the later value (checkbox) will overwrite a param value with that name. This should be done automatically for you, unless you are trying to parse the params manually.

How to show required field validation conditionally from a view

In My MVC 4 application, I have a Multi Select List Box, where I can select multiple values, I also has an Item New Role as one of the list items, which also refers to a model property NewRole.
So using Jquery whenever the user selections contain New Role, I will provide a text box to the user, which is bind to NewRole from model as given,
#Html.TextBoxFor(m => m.NewRole)
Which also has the following evaluation field.
#Html.ValidationMessageFor(m => m.NewRole)
And I will hide this text-box if the user selected options does not has the Item New Role.
Now the problem is even if I hide the div which contain the Text Box, it will try evaluating the required field validation.
What I require is When User Selects New Role and the User did not enter anything in the provided text Box then validate the required field property.
I know I can write a JQuery to show an alert when the div visible and does't has any value. But I want this default validation should happen on that condition.
is it possible?
One of the trick to avid certain client side validation conditionally ... you can use the IGNORE attribute of the validation ...
jQuery Validate Ignore elements with style
$("#myform").validate({
ignore: ":hidden"
});
If this is not what you are looking ... I will provide more specific information
Yes it is possible with RemoteAttribute. Take a look at this article:
http://msdn.microsoft.com/en-us/library/gg508808(v=vs.98).aspx
Keep in mind that this is NOT client side validation meaning there is an actual server post happening.
Try using the rules add and remove, when new role is selected add new rule which validates the textbox on other selection remove the rule from text box and hide it like you are doing:
http://validation.bassistance.de/rules/

selecting checkbox value in ruby on rails

I have some 5 static checkboxes in my html page.
And i want to get the values of a selected checkbox when i click on submit button.
Below is my code snippet,please correct me where am wrong.
for i in 101..105
if (params[":Prod"+i] == 'selected')
#userid=session[:userid].to_i
#prodid="Prod"+i.to_i
#prodname=params[":ProdName"+i].to_s
#price=params[":Price"+i].to_i
#qty=params[":qty"+i].to_i
#Products=Product.create :UserId =>#userid, :ProductId =>#prodid, :ProductName =>#prodname, :Price =>#price,:Quantity =>#qty
if #Products.save
redirect_to viewcart_path
else
render products_path
end
end
Note:My checkboxes id's are Prod101,Prod102,Prod103,Prod104 and Prod105 which i have defined static in my html page.
You can create dynamic hash keys similar to what you're trying, but the syntax is a little off. Use:
params[:"Prod#{i}"]
or
params["Prod#{i}".to_sym]
Note that the value is not "selected" unless you've set that yourself in the view. Depending on whether or not you're using helpers to create the checkboxes, and which helpers/gems you might be using, the values can vary. It's likely you'll get "1" for checked boxes if you haven't overridden it. You might get "0" for unchecked boxes, but by default browsers don't send anything for unchecked boxes, so the param might not be there at all. Some of the form helpers trick the browser into sending "0" by using hidden form fields. Also note that "0" is a true value in Ruby, so you should explicitly check for "1" or whatever checked value you've provided in the form. Check your logs and see what the form submits to be sure.

Custom Display Form with Custom Workflow button

I have created a new custom list form that will show 4 fields on the page from a Custom List called "Shipment". I have added Form Action button that I would like to run a custom action that is set inside of a Workflow. Currently, the form displays the fields for "Manifest Number", "Pickup Location", "Delivery Location", & "Scheduled Pickup Time". When the user clicks the Form Action button, what I want the Workflow to do is go to the ID field of the displayed content in the Form and change the value of the "Picked Up" column from No to Yes. The problem I am having is passing the ID of the displayed information from the Form to the Workflow as a variable. I can get the "Picked Up" column to update if I specify the value in the "Update List Item" window under the "Find the List Item" section, but I cannot figure out how to do this dynamically from the Form
You need to add a hidden field to store the ID in the custom list form. Copy the line that displays "Manifest Number" and change it to point to the ID field. And the change the variable name to ff6 or something like that (given you already only have 5 fields). Now when you open the custom action workflow in SPD the ID field should be available for your apply as a filter.
Hope this helps.

Resources