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

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.

Related

Adobe DTM: Event based rule to track form submission

I am trying to track form submission for a form using event based rule in DTM. I know that the ideal way to do it is to do via direct call rule but need to track the form fields as well through a data element and then into an eVar.
The form code looks something like this on the page:
<div style='display:none'>
<div id="popupform">
<div class="form_container">
<img src="/images/New-Offer-Pop-Up-Without-form_18-Jan-16.jpg" style="width: 100%;">
<form class="offerform" accept-charset="UTF-8" action="/site/sendtohs">
<div class="formrow"><input type="text" name="popup_fullname" class="new-input1 required" placeholder="Name" /></div>
<div class="formrow"><input type="email" name="popup_email" class="new-input1 required" placeholder="Email" /></div>
<div class="formrow"><input type="number" name="popup_phone" class="new-input1 required" placeholder="Mobile"
minlength="10" maxlength="15" /></div>
<input type="hidden" name="popup_url" value="/" /></br>
<p align="right"><input class="tbn newtbn" type="submit" name="submit" value="Submit" /></p>
</form>
</div>
<div class="offerform_success" style="display: none">
<img src="/images/Thank-you-pop-up-new-xyz.jpg" style="height: 440px; width: 100%; position: absolute;width: 99%;">
</div>
</div>
</div>
The even based rule in DTM is :
DTM Rule
The event fires when I click on the Submit button but the rule does not validates if the form fields have been filled or not. Any hints how can I add validation to the form submit event within DTM.
Targeting the form
Firstly, based on your screenshot vs. posted code, your event will not trigger because the form tag does not have an id attribute of "popupform" (or any id attribute at all) (You do have that as an id in a parent div, but that's not what you are targeting). So, you will need to remove that.
If you only want to target the form if it's within a div, then you will need to add it as a Rule Condition. Under Rule Conditions > Criteria, choose "Data > Custom", and click Add Criteria.
In the code box, add the following:
if ($(this).parents('#popupform').length)
return true;
return false;
Note: I'm using jQuery syntax here for brevity and easy cross-browser compatibility. The overall goal is this references the targeted form, and you want to check that it is within an html element with the popupform id. The jQuery above traverses up the form's ancestor chain to look for it. If it finds it, we return true. Otherwise we return false. Ultimately, custom rule conditions should return true if you want it to pass, or false if not. Also note that if you have multiple rule conditions (which you will; see below), all conditions must return true in order for the rule to trigger. If your site does not use jQuery or you do not want to use jQuery for this, then you will need to write your own code following the above concept.
Validating the form fields
Before I get into this, one thing I should note is in general you should not rely on client-side form validation for your forms. It is super easy to disable/get around it. You should be doing form validation with server-side code when the form is submitted, and then pop whatever you need to pop (form complete tracking or w/e) after it has been server-side validated.
Maybe you already have server-side validation in place but for whatever reason you can't control popping a dtm rule after validation, and this is your next best thing. Well hopefully the only thing you are doing is popping tracking and not using the dtm rule for actual site functionality, but even then, just want you to be aware that this is a "lesser evil" solution, not a "good" solution.
Having said that, you can again turn to a custom condition to make sure all the form fields are filled out. Here is a quick and dirty custom rule condition to demonstrate. Again, under Rule Conditions > Criteria, choose "Data > Custom", and click Add Criteria (so you will now have another custom code box). Add the following:
var isFieldsFilled=true;
$.each($(this).serializeArray(),function(i,v) {
if (!v.value||v.value=='')
isFieldsFilled=false;
});
return isFieldsFilled;
This code again uses jQuery to grab the form fields of the targeted form and loops through them and sets a flag to false if one is found not to have a value. Again, this is kind of quick and dirty and will work based on your current form example, but you may need to expand upon it in practice.

Finding the index of HTML element Capybara

I am trying to write the step definition, using Capybara, for a Cucumber scenario that is meant to confirm that checkboxes on the home page appear before the options.
Mechanically, I am trying to find the index of a specific html checkbox using its HTML ID and compare it with the index of a specific text on the home page. However, I have spent hours on this issue and have not been able to implement the step definition.
Would I for example be able to somehow convert the page into text and just search for words?
For this web application I am using Ruby on Rails.
Let: checkbox id = environment_Cool
Let: text on the page = Cool
I would greatly appreciate your help.
Suppose you have a checkbox like this:
<form action="demo_form.asp">
<span>
<input type="checkbox" name="coolness" value="Cool" id="environment_Cool" checked> Cool</span>
<br>
<span>
<input type="checkbox" name="coolness" value="Cool"> Not So Cool</span><br>
<input type="submit" value="Submit">
</form>
Mechanically, I am trying to find the index of a specific html
checkbox using its HTML ID
page.find('[#id=environment_Cool ]')
and compare it with the index of a specific text on the home page.
Then it should return the text at the parent of the checklist, which is "Cool".
puts page.find('[#id=environment_Cool ]').find(:xpath,".//..").text

Rails will_paginate custom renderer manual page number

Hy
What i want to do is to create a custom renderer for will_paginate which renders first, previous, next and last page and a input field where the user can type in the page number manually. I already have the links for first, last etc. but i stuck at the input field. I could create a form in the view but the input field has to be rendered between the previous and next links.
Does anyone know how to do this?
Thanks for your help
You can do this as a separate form (make sure it is a GET). All you
need is the one input element named page. Something as simple as this
should work (not all browsers may like the #). I dropped it into a
site I'm playing with now and it worked. Put it anywhere on your page.
You might have to make something more complicated if you need to
incorporate search terms.
<form action="#" method="get">
Go to page: <input type="text" name="page" value="" size="2"
maxlength="4" />
<input type="submit" name="btnSubmit" />
</form>

How do I create a Up/Down voting system like stackoverflow?

I'm making a site in Rails, and I want to add a vote up/down system like here in Stackoverflow.
Can anyone suggest how to do it? I do know that I'll enter each vote into the database, but I mean, how do I code the vote buttons? What will I use, can anyone help me. Ajax isn't required but it would be nice.
I was going to try to use a POST command and do something like this,
<form name="input" action="/grinders" method="POST">
<input type="hidden" name="id" value="<%=h grinder.id %>">
<input type="hidden" name="vote" value="up">
<input type="submit" value="Vote" />
</form>
But, I get an authenticity token error, and I honestly do not know how to work with the form helper.
Have a votes table like so:
[PK] vote_id, vote_type (up/down), [FK] post_id, [FK] user_id, time [optional]
Also add a score field to your posts table
Then you could have the vote button access a link like: /vote/post_id/type/, eg: /vote/14098/up. This can be done with or without Ajax.
When the vote action is called, check if the user has previously voted on that post - if yes, deny it. If not, create a row with the relevant values in the votes table and update the score field in the posts table.

asp.net mvc post to different views in same form

I have form area in my view. If I click button A, I want to submit to /Books/1 and if I click button B, I want to submit to /Books/2
How do I achieve this with MVC?
<form id="form1" name="form1" action="/Books/" method="get">
<input type="text" name="search" value="">
<input type="submit" name="id" value="1">
<input type="submit" name="id" value="2">
</form>
It sounds like what you want to do is call the Books Controller, with, say, the Search action. So for instance you might want to call /Books/Search/<search expression>/1, or /Books/Search/<search expression>/2, etc. (There's a few different ways you could be formatting these URLs, but it's mostly a matter of personal preference I think) If you want the URLs to appear as you've got them above (without the action in the URL), that can be accomplished with routing, something like this:
routes.MapRoute(
"Books",
"Books/{searchExpr}/{pageId}",
new { controller = "Books", action = "Search", searchExpr = "", pageId = 1 }
);
I think the main problem is that you're trying to use the WebForms PostBack everything paradigm in a situation where you're probably better off sending the information to the server in the URL or query string. The only time you're actually going to be posting form data here is when the user actually types something into the search box and clicks the Search button - at that point, the controller will pass the search expression to the appropriate View by stuffing it in ViewData, and from there, the View can pull it out and repopulate that textbox on the results page.
MVC Views can have multiple forms on a 'page', so just create separate sections and give each one their own form action.
<form id="form1" name="form1" action="/Books/1" method="get">
<!--...form fields-->
</form>
<form id="form2" name="form2" action="/Books/2" method="get">
<!--...form fields-->
</form>
I have never seen the ability to have a form field attached to two forms, seems like it wouldn't work. What you can do is put a hidden field in the second form which, on submission, grabs the information from the textbox in the first form.

Resources