I am beginner in ruby + rails.
I have problem to get values in controler of checkbox with params[] of rails.
The Case:
I have simple form that that have checkbox and i want to sent reqular request to controller action with user checked values.
The problem that params[:rating] can have few values.
My case:
html user side:
<form id="ratings-form">
<input name="rating" type="checkbox" value="G">G
<input name="rating" type="checkbox" value="PG">PG
<input name="rating" type="checkbox" value="PG-13">PG-13
<input name="rating" type="checkbox" value="R">R
<input type="submit" value="refresh">
</form>
controller action code to parse checked values: (get error 1. params[:rating] == nil or params[:rating] == string)
params[:rating].each do |rat|
p rat;
end
What should i change in the code to make it work?
Thanks
Try this HTML
<input name="rating[]" type="checkbox" value="G">G
<input name="rating[]" type="checkbox" value="PG">PG
<input name="rating[]" type="checkbox" value="PG-13">PG-13
<input name="rating[]" type="checkbox" value="R">R
Then you should have an array in params[:rating].
Related
I have a color filter, simillar to this:
<input type="checkbox" id="orange" class="orange" value="orange"/>orange
<br/>
<input type="checkbox" id="peach" value="peach"/>peach
<br/>
<input type="checkbox" id="terracotta" value="terracotta"/>terracotta
<br/>
<input type="checkbox" id="coffee" value="coffee"/>coffee
<br/>
<input type="checkbox" id="browne" value="browne"/>browne
<br/>
<input type="checkbox" id="rose" value="rose"/>rose
<br/>
<input type="checkbox" id="red" value="red"/>red
<br/>
I recive color from DB.
<div th:each="model : ${allColor}">
<span th:text="${model.color}"/>
Can I hide color, with use Thymeleaf, which is not in DB?
For example now I haven't Rose, Coffee and Peach colors, but maybe in future I will have this color. I want to do colors verification , if color is in DB, user can see on UI checkbox, else checkbox hide. I read about th:if and th:unless. Whether it is possible to make it with use Thymeleaf?
If I try to do:
<input type="checkbox" th:if="${model.color==coffee}"id="coffee" value="coffee"/>coffee
It's not working.
Instead of hiding colors, you should create the color checkboxes with a loop as well. Like this:
<th:block th:each="model : ${allColor}" th:with="color=${model.color}">
<input type="checkbox" th:id="${color}" th:class="${color}" th:value="${color}"/> <span th:text="${color}" />
<br />
</th:block>
As for why your original attempt didn't work, it's hard to tell without seeing more of the html. I'm guessing that ${model.color} was undefined, because you weren't in a loop? Also, you were missing quotes around 'coffee'. like this: `${model.color == 'coffee'
Something like this may work as well, but I would recommend the loop.
<input th:if="${allColor.contains('orange')}" type="checkbox" id="orange" class="orange" value="orange"/>orange<br/>
<input th:if="${allColor.contains('peach')}" type="checkbox" id="peach" value="peach"/>peach<br/>
<input th:if="${allColor.contains('terracotta')}" type="checkbox" id="terracotta" value="terracotta"/>terracotta<br/>
<input th:if="${allColor.contains('coffee')}" type="checkbox" id="coffee" value="coffee"/>coffee<br/>
<input th:if="${allColor.contains('browne')}" type="checkbox" id="browne" value="browne"/>browne<br/>
<input th:if="${allColor.contains('rose')}" type="checkbox" id="rose" value="rose"/>rose<br/>
<input th:if="${allColor.contains('red')}" type="checkbox" id="red" value="red"/>red<br/>}`.
Basically I have a collection of strings, and I want to render them as checkboxes on the page. To do this, I have written this code:
#for (var i=0; i< Model.AvailableCats.Length; i++)
{
<input type="checkbox" name="Cats[#i]" value="#Model.Cats[i]" #(Model.Cats.Contains(Model.AvailableCats[i]) ? "checked=checked" : "") /> #Model.AvailableCats[i]
}
This produces checkboxes like
<input type="checkbox" checked="checked" value="Bengal" name="Cats[0]">
<input type="checkbox" checked="checked" value="Moggy" name="Cats[1]">
When submitted this works ok if both are checked, or if the first is checked, but if only the 2nd item is checked, it's only submitting Cats[1] and MVC does not map this into an array.
I'm sure the answer is very simple but how can I submit my collection of checkbox values?
This is why Html.Checkbox actually adds a hidden input element in addition to the checkbox input. Checkboxes only submit a value if they are checked, so adding a hidden input with the same name, means that if it's not checked, something will be submitted, even if it's only an empty string.
<input type="checkbox" checked="checked" value="Bengal" name="Cats[0]">
<input type="hidden" name="Cats[0]" value="">
<input type="checkbox" checked="checked" value="Moggy" name="Cats[1]">
<input type="hidden" name="Cats[1]" value="">
Okay the answer was simple, just have the same name property on each one. It doesn't have to be unique and MVC binds it into a collection if more than one are submitted.
<input type="checkbox" checked="checked" value="Bengal" name="Cats">
<input type="checkbox" checked="checked" value="Moggy" name="Cats">
Surprised it took me that long.
I have JamboPay api that i want to integrate with my rails application. It looks something like this;
<form method="post" action="https://www.jambopay.com/JPExpress.aspx" target="_blank">
<input type="hidden" name="jp_item_type" value="cart"/>
<input type="hidden" name="jp_item_name" value="test shop"/>
<input type="hidden" name="order_id" value="455879"/>
<input type="hidden" name="jp_business" value="business#yourdomain.com"/>
<input type="hidden" name="jp_amount_1" value="51"/>
<input type="hidden" name="jp_amount_2" value="0"/>
<input type="hidden" name="jp_amount_5" value="0"/>
<input type="hidden" name="jp_payee" value="email#yourcustomer.com"/>
<input type="hidden" name="jp_shipping" value="company name"/>
<input type="hidden" name="jp_rurl" value="http://www.yourwebsite.com/testpost/Result.aspx?ii=0"/>
<input type="hidden" name="jp_furl" value="http://www.yourwebsite.com/testpost/Result.aspx?ii=1"/>
<input type="hidden" name="jp_curl" value="http://www.yourwebsite.com/testpost/Result.aspx?ii=2"/>
<input type="image" src="https://www.jambopay.com/jambohelp/jambo/rsc/paymentsbyJamboPay.jpg"/>
</form>
I want to be able to send this information from my transactions controller in the create method.
Any ideas how i can pass this form from my controllers because i keep the same form for all payment methods in the views.
Thank you in advance.
You can send this post request using Net::HTTP library all you have to do is to send this information to your controller action and then send post request from action.
I have the following code in my HTML.erb
<form>
<input id="do" type="hidden" value="0" />
<button type="submit" value="Next">SUBTRACT</button>
</form>
<form>
<input id="do" type="hidden" value="1" />
<button type="submit" value="Next" >ADD</button>
</form>
Now When I click the any of the 2 buttons, I want to check the value of the input id="do" inside my Ruby Controller. What would the syntax look like ?
This should optimally be separated into two actions in the controller where each of the forms would submit (using the action attribute).
<form action='add'>
<input id="do" type="hidden" value="0" />
<button type="submit" value="Next">SUBTRACT</button>
</form>
<form action='subtract'>
<input id="do" type="hidden" value="1" />
<button type="submit" value="Next" >ADD</button>
</form>
#in your controller
def add
# do the addition processing
end
def subtract
# do the subtraction processing
end
If you really need to implement them in 1 action, then you could use Rails params object.
You need to set the name attributes, the name attributes get sent to the controller in the params hash
<form>
<input id="do" name="subtract" type="hidden" value="0" />
<button type="submit" value="Next">SUBTRACT</button>
</form>
<form>
<input id="do" name="add" type="hidden" value="1" />
<button type="submit" value="Next" >ADD</button>
</form>
In the controller,
the params[:subtract] or params[:add] will have the value depending on subtract clicked or add clicked .
I want to make a program in C# which will go/login and do stuff on a website. I'm using Fiddler to see which URL should I use.
So, in Fiddler I write:
https://landfill.bugzilla.org/bugzilla-tip/post_bug.cgi?Bugzilla_login=mymail#hotmail.com&Bugzilla_password=mypassword&product=WorldControl&version=1.0&component=WeatherControl&rep_platform=All&op_sys=All&priority=P2&bug_severity=normal&target_milestone=World 202.0&bug_status=CONFIRMED&assigned_to=mymail#hotmail.com&short_desc=bla
And I send it with POST. I get a message which says: "Are you sure you want to commit these changes anyway? This may result in unexpected and undesired results."
Then, there is a button which says 'Confirm changes'. Its code in the result html page is:
<form name="check" id="check" method="post" action="post_bug.cgi">
<input type="hidden" name="product"
value="WorldControl">
<input type="hidden" name="version"
value="1.0">
<input type="hidden" name="component"
value="WeatherControl">
<input type="hidden" name="rep_platform"
value="All">
<input type="hidden" name="op_sys"
value="All">
<input type="hidden" name="priority"
value="P2">
<input type="hidden" name="bug_severity"
value="normal">
<input type="hidden" name="target_milestone"
value="World 2.0">
<input type="hidden" name="bug_status"
value="CONFIRMED">
<input type="hidden" name="assigned_to"
value="mymail#hotmail.com">
<input type="hidden" name="short_desc"
value="bla">
<input type="hidden" name="token"
value="aGipS2Hfim">
<input type="submit" id="confirm" value="Confirm Changes">
What should I write as URL in Fiddler or in browser to click this Confirm button?
You should submit POST data to URL https://landfill.bugzilla.org/bugzilla-tip/post_bug.cgi .
POST data should be as follows :
version=1.2&Component=WeatherControl& .... etc
Don't forget to encode POST data and set content type to "application/x-www-form-urlencoded"
UPDATE: When you receive first answer with confirm button, parse it as DOM and submit it again to same URL.This should behave same as you click on confirm button
The problem was that the parameters shouldn't be in the URL. This isn't GET method.