Which way to structure Data for User Generated Form Templates - ruby-on-rails

I'm thinking about an idea for something, as well as learning Ruby on Rails (easy eh :) ). I want something to allow a user to generate forms as templates, then assign these templates as forms in a location in a tree hierarchy, then allow users to fill in instances of these forms and save the data.
So, I've got two different thoughts as how to structure the data. I'm currently thinking a template will have one or more sections, and a section will have one or more fields and the field will then have attributes (type, color, position, validation etc).
So, should this be described as linked tables ;
field belongs_to_a section belongs_to_a template
or should I have one template table, that has a field called body that can be serialized in and out to XML or something to render ?
<template>
<section>
<field attrib=foo attrib2=bar><field>
</section>
<template>
Any ideas or comments welcome, even if I'm totally on the wrong track....

I would opt for the first design. If I were a user (or a developer) I'd much rather enter data in logically related tables than write XML to define a screen. In fact, Oracle have used a data structure not unlike this behind their very successful tool Application Express.

Related

Creating multiple objects of same model with a single form in rails

I want to create several instances of same model form a single form. And more importantly, the number of instances aren't known before form rendering.
I've seen several tutorials of this kind, but unfortunately those didn't suit my need. I've seen Ryan bate's nested form tutorial. But I'm not creating nested form. I've also seen some tutorials, which do create multiple objects, but the number of object's are all known in those cases. One of the tutorial is here - http://archive.railsforum.com/viewtopic.php?id=717
User will click a button and a new set of fields for a new object will be inserted just like the nested form demo from ryanb.
Here is a mockup of what I want. It's basically a very small form fit into a single line.
As i understand you need cocoon gem it allows to add form fields
It sounds like you may need to reach beyond Rails views and utilize javascript to dynamically render more "partials" when the user decides to add more fields. Something like this: Adding input elements dynamically to form
If you want to keep your view rendering logic in rails, you could make an AJAX request to your application, have it return just a partial's worth of html back, and insert the response html into your dom.

How to edit a model instance's text field (storing HTML) using markup/down editor?

(app is built on Rails 4.0.3/postgres)
I have a model defined where one of the attributes is a text field containing the entire HTML of a webpage- I store it as text, which I then set as an instance variable (#html) in the controller and then render it through a view using <%=raw #html %>. This allows me to store and render entire pages easily.
My question is, I need to allow users to edit the HTML in-browser using some kind of markup language/editor, so how would I go about doing so? The workflow would be that the user clicks on an instance of the model through a dashboard, and then is able to edit the name of model instance (easy), and under that is able to edit the html attribute and save it via some kind of markup editor like Github's gist editor. I feel like this should be easy but can't figure it out- can anyone point me in the right direction?
Thanks!

Using Ruby/Rails To Programmatically Post Form Data To Another Site

I'm trying to figure out a way to automate posting data on this site which does not have an API and thankfully not a captcha as well.
For example there is a form here => http://www.austinchronicle.com/gyrobase/EventSubmission
By examining the form I can figure out that the Name text box is setup as follows......
<input type="text" name="Name" id="EventName" value="" class="rejectPipe">
Using Ruby/Rails is there a way I can programmatically POST to the form on that page through the controller or in a rake task?
I sniffed out some links like => http://biodegradablegeek.com/2008/04/how-to-post-form-data-using-ruby/
and that seems to sort of explain the basic premise but what about inserting data into the time control or the drop down select boxes on another site? Or pretty much anything more complicated than inserting a string into an input box
Your best bet is to use something like Mechanize. I've written a blog post on a related subject (uploading data with Mechanize) : http://davidsulc.com/blog/2011/11/13/uploading-data-using-mechanize/
Alternatively, if you want to see the page while information is being entered, you could user Selenium http://davidsulc.com/blog/2011/11/27/automating-web-site-interactions-with-selenium/
You can use Typhoeus
For datetime selects you need conform to Rails protocol. Just pop open the source of the page and look at the names of their elements and use the same structure when posting. Do the same with select boxes

is it possible to nest one form inside another in razor?

I put the begin/end form statement in the layout page so that I don't have to repeat it on several pages. Below's a simplified version of the code.
#using(Html.BeginForm())
{
#RenderBody()
<input type = "submit" name = "nextButton" value = "Next-->" />
}
Things are working well. Unfortunately, I have a page that has several "Delete" buttons. I want to generate a form for each delete button so that it can send the id of the item to delete back to the controller.
Can I do that knowing that there's already another form on top of that?
Thanks for helping
As Mrchief says, the the HTML specs forbid nested forms. Since MVC just generates standard HTML, you have to work withinn the framework of the spec.
Why not just create two master layouts, and use the form based one most of the time, but use one without a form when you need more control over the embedded forms.
This is the why you should really only use forms exactly where they are needed, not just everywhere.
Do note that nesting of forms is not allowed as per the W3 specs
Every form must be enclosed within a FORM element. There can be
several forms in a single document, but the FORM element can't be
nested.
There is an interseting article about caveats of nesting forms here.
In this case, it is better to generate a form for each button instead of having a single form.
ASP.Net web forms restricted you from having multiple forms on the page by using runat=server attribute (and the framework ensuring that only one per page was allowed). MVC forms are pure HTML so you can have multiple of them.

What is the best way to handle repeating forms in MVC?

The best public example that I can think of off the top of my head would be the amazon shopping cart. Where you have a page that displays multiple distinct records that can have multiple distinct fields updated.
I can't put each one in a form tag because the user may modify more than one record and then submit.
I can't just update all the records that I get back because:
1. Performance
2. Auditing
3. If someone changed the record that the user 'didn't change' when they were viewing the page and then the user submits those changes would be overwritten.
So how to best handle getting the data back and then getting which records where changed out of that?
Is that clear?
Use binding! Don't be iterating the form collection in your actions.
Steve Sanderson wrote a blog post about how to do it. I wrote a blog post on how to do it with MvcContrib.FluentHtml. Both posts are very detailed and include downloadable code.
Generate your form in a repeater, and append an ID to the form elements that increments with each new form. Save the number of repeated form elements in a hidden field. Then in your controller, read the value of this hidden field - that'll be the number of forms to read. Then, in a loop, retrieve each form's fields by specifying the name of the field, plus the loop index appended to the name, as the key.
You can use some javascript logic to detect when a form's value changes, and update a hidden field in that form's section if that occurs; or you can hide the original values inside a hidden field with each form section (although I don't recommend this as too many fields / forms will bloat your page).
one (but not necessarily the best) approach is to store which items are changed in a js-variable or something on the client side as they are changed, and then only send the data that is actually different from what the user recieved.
and as Erik stated, you could use hidden form elements to make sure that it works without js as well.

Resources