Hybrid Model/Controller Validation? - ruby-on-rails

I am looking to have a user enter in a URL to an image on a form. I already have this built. I would like for two different things to happen, but I was wondering if I could somehow combine them.
When the user submits the form, I would like the URL to be validated, not only that it takes on a valid url format, but that it actually exists.
I would also like to set an integer in the database depending on the type of document the URL points to. If it is an image, I would like an integer field on the record (let's call it 'file_type') to be set to 1. If it is not an image, I would like the validation to fail.
Is there any way to validate the URL while also collecting data for the record? I ask this because I believe it makes much more sense for the server to make the request once rather than twice, once for validation, and once for storing file type.
Is this possible? What would you suggest?
Thanks!

Related

Send only the key of the check_box_tag if checked

I have a bunch of filter categories represented by checkboxes, right now it's sending the full name + on in the query_params.
/?max%5D=5&Movies=on&Art=on&Outdoors=on
Instead, I'd rather just look for the presence of the key to know it's there. This would make the url a bit shorter for people to copy/paste.
/?max%5D=5&Movies&Art&Outdoors
Or, can I have one category key with all the checkboxes as values?
I think it's not possible by default, you'd have to construct an onsubmit script to build the URL and redirect to it, rather than posting directly to the server. You might be better off leaving it as it is.

Can someone explain Rails behavior for multiple fields with identical names and ids?

I needed to create a input mask for a jQuery datepicker in my Rails app, where the first form field uses m/d/yy format and the datepicker populates a hidden input with the proper database format.
I was using SimpleForm, and so I extended my own input so that the input is preceded by the mask.
I got everything set up and when checking out the browser, it all just worked well before I thought I would be done.
The form ends up with two inputs for the same attribute, each with the same id and name. I never thought this would work. Checking the development log I only see one date getting submitted, the second of the two which is the one that has the proper format for the database.
Is this all okay? Should I take some extra steps even though this appears to work fine, and more importantly, can someone explain what's going on under the hood that results in this behavior?
Thanks!!
Rails uses the Hash params to store fields submitted. When you declare two or more inputs using the same name, it happens the same as if you do something like
h=Hash.new
h[:name]="foo"
h[:name]="bar"
Result is bar because the foo was overwritten. So the "winner" is always the field value which the browser last appended to postdata.
But if I where you, I would not rely on the browser that the second field gets appended at last.

Rails validation fails but object memory remains unchanged?

First time user long time reader. I have thoroughly looked for an explanation for the problem I'm having via the mighty search engine Google, but alas I have failed to produce any significant insight.
I need to be able to ensure that a model form is not reloaded with invalid data. Since the model stored in memory on the server is edited directly with the parameters of the web form first, and THEN checked for validity, without additional code invalid model data will ALWAYS be sent back to the form. This is less than desirable to me. My question is this: how do I ensure this doesn't happen?
What I'm thinking is I need some mechanism for saving the state of the object before it's modified with the parameters sent from the web form, and then after a failed validation restore the object to it's previous, correct and unmodified state of being.
Help!
Thanks,
Les
The object isn't actually modified in the db if validation fails, even though the object is in an invalid state in the form ... the thinking behind this is that the user wants to see the errors they made so they can correct them.
If you don't want that to be the case, then just read back the object with a WhateverObject.find(x) and assign it to the variable that the form is referencing and it will 'restore' the object to its previous unmodified state.
To add to what concept47 said you can also get the value for a particular field using
object.field_was
Have a look at ActiveRecord::Dirty for details (http://ar.rubyonrails.org/classes/ActiveRecord/Dirty.html)
Using that you could retrieve the original values for just those fields that had validation errors.

how to return url in form file in strut

hai all,
i have one problem regarding to return the form file value is validation fail. i have one form that have a few field to be fullfil by the user and one attachment field.if one of the field is blank the system will give the error when submit the form..my problem is, all the field will have the value that we entered before this but for form file it disappear.
Let's see if I understand your issue: You have a form with several input fields, and among them a <input type=file> for uploading some file content. In case of validation error, you return to that page (with struts2 result = INPUT, I guess) and the content of the previously filled fields appears, except for the FILE field.
This makes sense, if you understand how the other fields are "refilled" in Struts2 in this scenario (just from the action, which has typically mapped the parameters to properties). The server doesnt know the (clientside) fullpath of the file, it is not sent along the http request (it would be a privacy issue), it's the content of the file what is uploaded (and perhaps the filename without path). You can't escape that.
Anyway, I think this a case in which validation should signal an error early (in javascript, in the client side). Think about it: the user is uploading a file (potentially large) together with other info, and AFTER uploading it the server checks the fields and instructs the user to refill the fields and retry (including the upload). This can be unacceptable. My advise is, then, to include client-side validation (and if it pass, and the server validation fails, then the user must resign to refill the FILE field). If the file is large, one could split the input form in two pages, a first one to fill the several fields, and afterwards another to upload the file.

Fill a rails form with a hashmap

I have a difficult situation.
I let the the user create a form through a Rich Text Editor and then I save this.
So for example, I save this literally into my DB:
http://pastebin.com/DNdeetJp (how can you post HTML here? It gets interpreted, so now I use pastebin...)
On another page I wrap this in a form_tag and it gets presented as it should be.
What I want to do is save this as a template and save the answers as a hashmap to my DB.
This works well, but the problem is I want to recreate what checkbox/radiobutton/... is selected when the user goes back to the page. So I want to fill the form with the answers from the hashmap.
Is there a way to use a 'dummy' model or something else to accomplish this?
Thanks!
Since you're pasting in raw HTML which is not properly configured as a template, it is more difficult to enable the proper options based on whatever might be stored in your DB.
The reliable approach to making this work is to use Hpricot or Nokogiri to manipulate the bit of HTML you have and substitute values accordingly. This isn't too hard so long as you can define the elements in that form using a proper selector. For example, create a div with a unique id and operate on all input elements within it, comparing the name attribute with your properties. There may even be a library for this somewhere.
The second approach is to use JavaScript to enable the options in much the same fashion. This seems like a bit of a hack since the form itself will not have a proper default state.

Resources