This question already has answers here:
How to make a check_box checked in rails?
(11 answers)
Closed 2 years ago.
Background
In my app, users can choose multiple languages which they're able to speak on their profile edit page (users/edit.html.erb) from checkbox. Many-to-many relation is already written, so multiple data can be saved properly.
Now, when users came back to their profle edit page, all the checkbox are displayed in a blank. I would like to display languages with "checked" which user chose and saved.
users/edit.html.erb
<li>
<div class="col-1">Spoken languages:</div>
<div class="col-2">
<label><input type="checkbox" name="language[]" value="1">Japanese</label>
<label><input type="checkbox" name="language[]" value="2">English</label>
<label><input type="checkbox" name="language[]" value="3">Chinese</label>
<label><input type="checkbox" name="language[]" value="4">Spanish</label>
</div>
</li>
What I tried to do
I searched this kind of function, but I only found how to make a specific initial value (such as always check "English"). I would be grateful if someone give me advice.
Version
ruby 2.6.4p104
RubyGems 3.0.3
Rails 5.2.3
I was able to figure it out with include method. Thank you very much.
<label><input type="checkbox" name="language[]" value="1" <%= "checked='checked'" if #user.language_ids.include?(1) %>>Japanese</label>
<label><input type="checkbox" name="language[]" value="2" <%= "checked='checked'" if #user.language_ids.include?(2) %>>English</label>
<label><input type="checkbox" name="language[]" value="3" <%= "checked='checked'" if #user.language_ids.include?(3) %>>Chinese</label>
<label><input type="checkbox" name="language[]" value="4" <%= "checked='checked'" if #user.language_ids.include?(4) %>>Spanish</label>
Related
I have checkboxes value with Orange, Apple, Mango, Peach and I checked Apple, Mango Click on Submit and saving properly to PostGRE DB as {Apple, Mango}
When I tried to display it in UI its not displaying properly
<c:forEach items="${checkedFruits}" var="list1">
<c:forEach var="list" items="${allFruitlist}">
<div class="form-check">
<input class="form-check-input" type="checkbox"
<c:if test="${list1.fruitname== list.fruitname}"> checked="checked" </c:if>/>
<label class="form-check-label" value="${list.fruitname}" name="fruitname">${list.fruitname}</label>
</div>
</c:forEach>
</c:forEach>
İf you looking at https://www.javatpoint.com/spring-mvc-form-checkbox you can solve the problem.
I am having the following form, which I would like to submit to my backend.
<form id="revisionFilter" action="{{ route('revision.filter') }}" method='POST'>
<input class="form-check-input" type="checkbox" value="" id="checkbox1">
<label class="form-check-label" for="checkbox1">
1
</label>
<input class="form-check-input" type="checkbox" value="" id="checkbox0">
<label class="form-check-label" for="checkbox0">
0
</label>
<label class="form-check-label" for="checkboxNull">
<input class="form-check-input" type="checkbox" value="" id="defaultCheck1">
<label class="form-check-label" for="checkboxNull">
Null
</label>
<input type="hidden" name='_method' value='POST'>
<input id="revisionFilterSubmit" type="submit" class='btn btn-danger btn-sm' value='Filter'>
</form>
My backend looks like the following:
routes:
Route::post('/revision/filter', 'RevisionController#filter')->name('revision.filter');
RevisionController:
public function filter(Request $request)
{
Log::info("Request: ");
Log::info($request);
return redirect()->route('revision.rindex');
}
My problem is that when I press the button I am getting redirected to:
The page has expired due to inactivity.
Please refresh and try again.
Instead I would like to see the request with the values of the checkboxes to implement the db saving functionality.
I appreciate your replies!
since I've already made a comment. I will put my answer in the answer box lol.
To answer you, you're not getting any checkbox value since you're checkboxes don't have any name attribute.
You need to put a name attribute to them. E.g.,
<input class="form-check-input" type="checkbox" value="" name="checkbox0" id="checkbox0">
From there, you should now be able to see the value of the checkbox via its name.
Just put same name to your checkbox.For example
<input class="form-check-input" type="checkbox" value="" id="checkbox1" name="revisionFilter">
<input class="form-check-input" type="checkbox" value="" id="checkbox2" name="revisionFilter">
SonarQube 5.3
A recent security scan of our SonarQube server from our IT department states:
"AutoComplete Attribute Not Disabled for Password in Form Based Authentication"
I've tested this out in 3 different browsers (FF, Chrome, IE 11) and none of them autocomplete the password (although FF does autocomplete the login id).
Is there anything I can configure in SonarQube to satisfy the scanner?
Does SonarQube take any other steps that prevent auto-completion of passwords, so that this can be flagged as a false positive?
Cheers,
Dave
Scanner complained about this code:
<form id="login_form" action="/sessions/login" method="post">
<input type="hidden" name="return_to_anchor" value="">
<h1 class="maintenance-title text-center">Log In to SonarQube</h1>
<div class="big-spacer-bottom">
<label for="login" class="login-label">Login</label>
<input type="text" id="login" name="login" class="login-input" maxlength="255" required
placeholder="Login">
</div>
<p class="big-spacer-bottom">
<label for="password" class="login-label">Password</label>
<input type="password" id="password" name="password" class="login-input" required
placeholder="Password">
</p>
<p class="big-spacer-bottom">
<input class="login-remember-me" id="remember_me" name="remember_me" type="checkbox" value="1" checked>
<label for="remember_me"></label>
<label for="remember_me">Remember me on this computer</label>
</p>
<p class="text-right">
<button name="commit">Log in</button>
<a class="spacer-left" href="/">Cancel</a>
</p>
</form>
I second Ann's question, which tool did you used ?
I've just checked and SonarQube 5.3 does have the parameter autocomplete=off on login page. So I'm wondering if you are talking about another page or if it's a false positive of a security scan.
In my create.blade.php I have the following code:
<div class="form-check">
<label for="active">active</label>
<input id="active" name = "active" type="checkbox" class="form-check-input" value="1">
</div>
And in my edit.blade.php the following:
<div class="form-check">
<label for="active">active</label>
<input id="active" name = "active" type="checkbox" class="form-check-input" value="{!!$company->
active!!}">
</div>
SQLFiddle of my db structure with data.
When submitting either with the checkbox checked they work fine
When editing a record with active set to 1, the checkbox isn't checked when opening the form
I know there's no value when not checked and when opening the checkbox isn't 'on' because the value is 1 instead of 'on'.
How can I resolve this problem?
It's actually an HTML problem, you are just using the input the wrong way, you have to use the checked instead of value:
<input
id="active"
name="active"
type="checkbox"
class="form-check-input"
checked="{{ $company->active ? 'checked' : '' }}"
>
I want to check the first checkbox with id=user_accepts_terms. This is the HTML:
<div class="check-group">
<div class="checkbox">
<input type="hidden" value="0" name="user[accepts_terms]">
</input>
<input id="user_accepts_terms" type="checkbox" value="1" name="user[accepts_terms]">
</input>
<label class="" for="user_accepts_terms">
</label>
</div>
<div class="checkbox">
<input type="hidden" value="0" name="user[subscribed]">
</input>
<input id="user_subscribed" type="checkbox" value="1" name="user[subscribed]">
</input>
<label class="m-focus" for="user_subscribed">
</label>
</div>
I want to check the first checkbox with id=user_accepts_terms. Tried this among other things, but no luck:
find('.check-group').all('.checkbox')[0].find("#user_accepts_terms").set(true)
The .find("#user_accepts_terms").set(true) doesn't work, it says unable to find the css.
This piece works as follows:
2.1.0 :097 > find('.check-group').all('.checkbox')[0].text
=> "I accept the terms of use and privacy policy"
The .all('.checkbox')[0] portion is already finding the checkbox you want, and the .find("#user_accepts_terms") portion is trying to find another element below that, which doesn't exist. Either of the following should work, provided the syntax is correct (I'm unfamiliar with it)
find('.check-group').find("#user_accepts_terms").set(true)
find('.check-group').all('.checkbox')[0].set(true)