Form params not getting passed - ruby-on-rails-4

What are some reasons form params would not show up in the console after form submit? I have a form with nested attributes, that has been working fine. Suddenly, the nested params stopped saving to the database. Running debugger in the create action reveals that the nested params are not being sent with the form.
Strong params are set correctly, and were working before.
Im using .new in the controller action to build an instance of the nested objects
I did end up building the id and name attributes of the inputs manually due to some functionality I was looking for...but, again...this was already working.
Im using rails 4.0.2.
Are there any other aspects I could be missing as to why my nested fields are not showing up in the debugger params?

Turns out this error was from a misplaced html closing tag. Sometimes the form elements got passed and other times they didn't. It was a hard one to track down, because I was looking at active record and rails...when I should have looked at html as well. I finally found it because I was debugging the code in the browser and noticed that the form elements were outside of the form tag.

Related

Rails: Template is missing / one method for handling POST and GET requests?

I am a Rails newb, and am simply trying to flash an error, on a view with a form. When I sent a POST request to the path of the view I would like to flash the error on, my routes.rb file sends the POST request to my update method in my SchoolApplicationsController. However update is viewless so when I make the POST request to go there, and I meet an error case, I get Template is missing from my Rails Server.
The view where I would like to flash the error is the view which made the request to POST called the pay method in the same controller. What is the common practice here? Do I route the POST for the form submission to the same method as I would do the GET for the pay view, so that any flash errors can be presented in the desired place?
To render a view in a "viewless" action you need to use the render method. In your case render :pay. It will render the form, already filling all the fields, filled previously (if you match the instance variables right).
To flash errors in that form you may use one of several options. First is to simply use flash.now['error'] = 'Whatever error message you need' before you do the rendering. Second is using the .errors method in the instance variable you are updating from the form. Look up the docs or use the console to look up what data that method provides.

Django form with autocomplete functionality

I am working on a django project with complex forms. In one of my form fields I need following functionality...... Its the text field. As the user starts typing the value the suggestions from existing database should appear in dropdown. Can anyone help me out with this ? Just similar to autocomplete but able to add new values.
This is going to be something in the JQuery/AJAX side of things, not Django. I would read up on the autocomplete functions of JQuery and use AJAX to call your DJango code and receive a populated list, which then displays to the user.
JQuery Autocomplete - Custom Data
If you don't want to deal with JavaScript, you can use a django application called django-autocomplete-light.
You can learn more about it (and get it) here: https://github.com/yourlabs/django-autocomplete-light

How to override/update information from POST when creating model

I have a view that handles a POST request and attempts to create a new object. However, I know that some of the POST'd data is invalid... But I want to fix it and go ahead and create the object.
The only way I can figure out to be able to 'fix' data in a ModelForm is to create a 'is_valid()' form. To do this, I can either create the form with the POST data, or I can create it with an already existing instance. Unfortunately, if I use the POST data, because some of it is invalid, the form won't validate and I am thus unable to get to the data in the form to fix it. If I create it with an already existing instance, this works, but when the form is displayed, any remaining errors are for whatever reason ignored (and thus don't show up on the web page.) I've tried a combination of creating the the Model form from the POST data and giving it an instance, but this doesn't seem to help. Additionally, I've tried modifying (a copy of) the POST data, fixing it, and then creating the ModelForm from the 'fixed' POST data. This sort of works, with the exception that I have some ImageFields in my form, and they seem to just be ignored.
Any help would be greatly appreciated. I have looked at every good page that I can find to no avail.
Perhaps there is a better way to do this? The problem I'm trying to solve is that I want to have a model that contains ImageFields. The first time I put up the form, the user needs to 'upload' images for each of the fields. However, if he doesn't update an image for one of the fields, I want the new form to come up with a Image upload button on the fields where images have not been uploaded, and just a text field with the image name for images that have been uploaded.
Edit 9/15/2010:
Ok, I think I can simplify all of the above question into this:
def testing( request ) :
test_form = UserProfileForm()
valid = test_form.is_valid()
return render( 'testing.tmpl', locals(), request )
When the above code is rendered, the 'valid' shows as False (as one might expect), but the 'test_form' renders without any errors. I've read through (if perhaps not understood?) the documentation on Models and ModelForms, and I see that most of the time a ModelForm (in my case: UserProfileForm) is created with a specified 'instance'. However, 1) I don't have an instance yet, 2) I would still expect the non-instance'd Form to display errors. I'm sure there is something I am missing. Please illuminate. :)
One more thing, which perhaps the answer to the above will answer anyway, but as far as I can tell, the is_valid() call is supposed to call the 'clean()' function I defined for the UserProfileForm. However, (not being a python guru) I placed 'raise ValidationError()' at the top of clean(), and when I run the code, no error is shown. Thoughts?
Update: I figured out the problem and the answer is below. Thanks!
You should have a look at how to clean form fields in django. You could either manipulate the data returned from the form there or make any kind of validation!
If your ImageFields are optional then you can still validate them (that they are otherwise correct).
Then it's a matter of adjusting your template to show either the uploaded file name or an file upload field depending on whether they've already uploaded one or not. Actually, it would probably be better to give them both fields in the first case. That's what the automatic admin does (the upload field is labeled "Change").
Well, after figuring out how to use the python debugger (pdb) and the fact that within emacs it kind of 'just works' (wow!?) I was able to find that my (empty) form was not bound. Googling bound forms pointed me to this page:
http://docs.djangoproject.com/en/dev/ref/forms/api/
RTFM'ing I find that I can pass an empty dictionary to my form and then everything starts to behave as I would expect. So, to summarize, there is a big difference between:
test_form = UserProfileForm()
and
test_form = UserProfileForm( {} )
The second version causes the rendering of the form to show all the errors (and to call 'clean()').
With risk of having this deleted by the moderator ;) Thank you to all those who commented and for your patience with a new django developer.

drupal theming a view for Customfield: PHP code

I have a view called "contests_slider" with a block display. I'm hiding all fields and using a "Customfield: PHP code" field instead which calls a function called display_front_contests(). In that function, querying the database and building some html and returning it. I'm displaying the output in a block. The problem is Drupal is adding alot of extra divs that I don't want. I went to "Theme: Information" and copied the theme "views-view-field.tpl.php" to "views-view-field--contests-slider--block-1--phpcode.tpl.php" and put just: in it and it's still outputting all the extra html.
Any ideas? am I using the wrong template?
If you are only using views to create a block, but otherwise query the datebase, create the markup etc, you should consider making a block in a custom module. All the work is in the code you have already written. That way you wont have to think about the many templates that views uses, but instead you'll just use the block.tpl.php.
Take a look at hook_block for info on how to do it.

django - returning form errors from a generic application view

I am developing a generic application. Let's assume that it handles Foo objects which can be attached to any model.
In a template, a Foo form can be shown by the get_foo_form template tag:
{% get_foo_form for object as form %}
object is the object which foo will be attached to.
The form posts to a view in the foos application. if the form is valid, everything is fine. the new foo is saved and the view redirects to the former page (via a 'next' argument hidden in the form) and the new foo is displayed nicely.
But if the form is not valid, I'm lost. For the same case, a similar application, the django.contrib.comments has an intermediary page that asks the user to correct the errors. However, I don't want to display an intermediary page, I want to show the former page with the errors. A redirection does not suffice here as I need to pass the error message(s) to the former page.
Is there a way to accomplish what I'm trying to do, or should I change the whole structure?
All you really need to do is to add the error messages to your dictionary when going back to the page you were at. Then in your HTML, add if tags to check if there are errors and display the appropriate errors if need be.
You could store your error messages in a list and then have that as one of the dictionary parameters in a call to render_to_response.
If you have an app featuring a generic model, the best way to pass error messages to the posting view after validating the form in the post handling view seems to be using the session to store the messages and redirecting back.
Thanks and +1 to AlbertoPL for his help.