What to add in my controller in Grails? - grails

I'm doing an application where after log in, User gets some settings (in check-boxes) which are Boolean in my database.
I'm trying to make that every User have his own settings checked, apropose when User logs checked are those fields where the value in the database is 1. My check-box is:
<g:checkBox name="task" value="${setting?.message}" />
<label class="text2" for="calendar">Things to done</label>
What I have to write in my controller?

in your scenario i understand there exist a one to many relationship between user and setting, as you explain after user successfully login user is take it to a view where are listed all possible setting.
you can do this in the view
<g:each in="${settings}" var="setting">
<label class="checkbox">
<g:checkBox name="settings" value="${setting}" checked="${user?.settings?.contains(setting)}"/> ${setting}
</label>
</g:each>
i took this code from a similar scenario i solve someday ago you can see it here in this github repo
after sending the form in the controller you can handle in this way
def settings = params.list("settings")
you can see the full action controler example here
You could now apply some user.addToSettings or user.removeFromSetting in order to save selected settings
i hope it help you

Related

Dropmenu with if clauses

I am trying to make my dropmenu whenever I select the number 30 to print hello,however it always shows Hello,any idea why it doesn't work
<g:select name="user.age" from="${18..65}" value="${age}"
noSelection="['':'-Choose your age-']"/>
<g:if test="${age==30} ">
Hello
</g:if>
That should be <g:if test="${user.age==30} ">. The problem here is as per your last post, a user changing the value of a select box on the front end has not been registered or sent to your grails controller. When you next do <g:if It is checking the same value as was provided by your controller in the instance of
<g:select name="user.age" from="${18..65}" value="${age}" age here.. again this is wrong since you have called name user.age
So unless your g:select on change submits it to the controller then your gsp is reloaded so <g:if test="${user.age==30} "> can now pickup the new value.
It is a static value received by your view at the time controller called it VS your user dynamically changing the content of the select box. For which the change behaviour is handled by jquery as explained in previous answer to your previous question

checkbox is checked activeAdmin

I got a problem...
I had to use active admin for create a custom page -_-.. So I can't interact with my controller from my view.
So I've create many checkbox like this in my view,
input type="checkbox" class="filter" autocomplete="off" name="lundi" value="0" checked>Lundi
I'd like to use them in the same view I want to know if the checkbox is checked or not. So if someone ad a tricks? I'm a new user of rails.
You can use jQuery to check if checkbox is checked or not and do some thing on that decision, send ajax call or something else.
if ($("[name='lundi']").is(":checked")){
console.log("Do Something.");
}
Let me know if you need further help.

how to uncheck a check box in a Ruby form at load time

I'm using Ruby on Rails to build a form that has a checkbox ; the behavior I'm trying to achieve is the following:
the user loads the form
the user fills in the form and enables the checkbox
when "Submit" button is clicked, some actions will be executed (because the checkbox is checked)
if the user loads the form again, the checkbox will be unchecked, so no actions are executed
the user can fill in the form again with new values and submit them, if desired
The behavior I'm seeing is that that in step 4, the actions are executed again immediately at the time the form is loaded, and this is undesired, because these actions have already been executed in step 3. For info, the actions I'm executing are also embedded in the same partial view file than the form itself. This is probably an improper way to do it, but it works fine, as far as the execution of actions is concerned.
I tried many things and tried all combinations of checked unchecked, the only one that displayed the box as unchecked is :checked=>"unchecked", but the behavior is still not the one I'm looking after.
What is the best way to implement this scenario?
The checkbox is configured as follows:
<%= f.label "CheckToApply" %>
<%= f.check_box :CheckToApply, :id=>"CheckToApply", :name=>"CheckToApply", :checked=>"unchecked", :value=>"0" %>
and compiles to the following:
<label for="test_Check to Apply">Check to apply</label>
<input name="CheckToApply" type="hidden" value="0" /><input id="CheckToApply" name="CheckToApply" type="checkbox" value="1" />
I tried many ways, like setting the value in the partial view
if #test.CheckToApply == 1
#test.CheckToApply=0
<... more actions are added here ...>
end
I tested by printing something inside the "if end" section and it seems to execute well, but it did not work, because every time the form is loaded, the actions are executed again before the user chooses the new values.

Double CheckBox Creation mistake?

I have a foreach wich populates a table and one field of the line tables is:
<% foreach (var item in Model.List){%>
<td align="center">
<%: Html.CheckBox(item.ID.ToString(),item.isChecked)%>
</td>
<%}%>
Inside my post function I was trying to get the Request.Form["45"]( 45 is a sample ID) and saw that the value "true,false" was being recieved.
Taking a look into the Code generated, I just saw that:
<input name="45" type="checkbox" value="true" /><input name="45" type="hidden" value="false" />
How its possible since Im just asking to generate one input? I dont know too if Html.CheckBoxForis better to use in this case
Thanks !
You can read the explanation here
the same approach that both Ruby on Rails and MonoRail use.
When you submit a form with a checkbox, the value is only posted if
the checkbox is checked. So, if you leave the checkbox unchecked then
nothing will be sent to the server when in many situations you would
want false to be sent instead. As the hidden input has the same name
as the checkbox, then if the checkbox is unchecked you'll still get a
'false' sent to the server. When the checkbox is checked, the
ModelBinder will automatically take care of extracting the 'true' from
the 'true,false'
Why don't you use model so that model take care of it. Using request object is not recommended in asp.net-mvc You may also use formcollection parameter in your actionresult and handle this scenario using this approach.
You may always use html tags if helpers don't fit in your requirement.

How do I change the records being displayed on the page with buttons on the page (RoR)

I have a rails app that shows statistics based on an employee's daily production. currently my page shows ALL records.
I know how to show various different combinations such as records made between two dates etc... but what I really would like to do is make it so that single page (say the index page) has 3 controls that allow for it to switch between daily statistic records, weekly statistic records and a custom date constraint of statistic records (such as from date xx/xx/2009 to date xx/xx/2010). I have been searching for a while to attempt to figure this out but I am obviously missing something as I cannot find anyone else who has run into the same issues.
If this is too difficult to do this way, the other - mostly easy way I can see to do this is to create a page for each view, however it still leaves a question on how I set up the control to choose the custom date constraints.
I aplogise in advance for my newbyness but if someone could explain this to me, I think it is going to really increase my understanding of rails. Thanks in advance
Your control can easily append some information to the query string. Like this form does:
<form action="" method="get">
<fieldset>
<button type="submit" name="show" value="daily">Daily stats</button>
<button type="submit" name="show" value="weekly">Weekly stats</button>
</fieldset>
<fieldset>
From <input type="text" name="interval-start" value="" /> till
<input type="text" name="interval-end" value="" />
<button type="submit" name="show" value="interval">Stats for the specified interval</button>
</fieldset>
</form>
When user clicks on some button, browser sends all form fields to the server-side. On the server-side, in your action, you can check for the show property of the params array. Like this:
#reports = case params["show"]
when "daily"
#Just an example. Use ActiveRecord's query interface or Arel in Rails 3, not plain queries
Report.find_by_sql "SELECT * FROM reports WHERE DATE(report_date) = DATE(NOW())"
when "weekly"
Report.find_by_sql "SELECT * FROM reports WHERE WEEK(report_date) = WEEK(NOW())"
when "interval"
Report.find_by_sql "..."
else
#Some other possible values, filters, so on
end
And in your view just output the #report variable as usual.
You could also create a new route for that filtering, maybe /reports/:name, and avoid using forms. In this case, you only create some links that point to the right filter (/reports/daily, /reports/weekly, etc). On the server-side you need to check reports[:name] for the appropriate value.

Resources