Best way to reload parts of a form - ruby-on-rails

I want to reload a part of a form generated with the form_for-helper via AJAX.
After reloading the part I still want to have access to the form object.
How can I do this?
Best regards

I am not sure if your are using different terminology than I've heard, but what do you mean "still want to have access to the form object"?
Do you mean access to it in JavaScript? That should still work as long as you don't overwrite the form tags.
Do you mean in the html.erb code generating the partial? That doesn't really make sense, because that form_for object has already generated its html tags and gone out of scope. You need to use to the regular form of the helpers that takes the name of the object as the first parameter. There is no problem with this working with the tags generated by the form_for version of the helpers.
So, in your main page:
<%= form_for :person, #person, :url => { :action => "create" } do |f| %>
<%= f.text_field :name %>
<div id="reloadable">
</div>
<% end %>
And in your partial that fills that div:
<%= text_field :person, :name %>
No step 3.

Related

Adding a confirmation checkbox to a form in Rails not tied to some attribute?

Noob question! :)
I have a form, that has basically no point other than call some_action. The reason I use a form for this, is because we have a specific styling for this in our large website.
<%= styled_form_for(#user, :url => some_action_user_path #user)) do |f| %>
<%= f.save_button %>
<% end %>
I thought, since it's a form, I should be able to put a checkbox in there. It should have no other goal than confirming the user wants to do this action indeed. E.g. "Yes, I want to do some_action to the user model".
How would I make a checkbox that does not really change any attribute or affect anything - Other than that it should be checked for the form to submit?
This is probably dead simple, but according to the documentation and various error messages I should provide arguments such an attribute (which I don't want...)
form_for is meant to work on attributes of a model, which is what all the documentation you are reading is telling you. So if your model had a boolean column you could easily attach a check box to it.
If you ever want a form (or specific tag) that does not follow this, you can use the _tag version of these methods. For example, form_tag or, in your particular case, check_box_tag.
Example:
<%= styled_form_for(#user, :url => some_action_user_path #user)) do |f| %>
<%= check_box_tag "do_some_method" %>
<%= f.save_button %>
<% end %>
NOTE: You will only get a param entry for :do_some_method if it is checked off. If you want to get a param regardless, you have to add a hidden_field_tag before it.
<%= hidden_field_tag "do_some_method", "no_dont_do_it" %>
<%= check_box_tag "do_some_method", "yes_do_it" %>
Now if the checkbox is selected you'll get params[:do_some_method] set to "yes_do_it"; if it's not checked off, instead of getting no entry, you'll get params[:do_some_method] set to "no_dont_do_it".

edit form submitting to the wrong url and remote: true failing to be picked-up

This has been driving me nuts because it doesnt seem to make any sense.
I want to do something relatively simple.
Display an edit form in a modal on the index page.
I have the following code looping through a collection of sites
<%= render(#sites) %>
<%= will_paginate #sites %>
Within the sites partial i have the following form hidden away
<%= simple_form_for site, remote: true do |f| %>
<%= f.input :name %>
<%= f.input :matter %>
<%= f.submit "Save", :class => "button gr thirt", id: "site_save" %>
<% end %>
instead of generating the expected HTML i get the following, linking to the show action, am I missing something fundamental here?
<form accept-charset="UTF-8" action="/sites/1" class="simple_form edit_site" data-remote="true" method="post" novalidate="novalidate">
</form>
I was looping through a collection of #sites, a results returned by a call to Site.all
so the object being served to the above form is one of the |site|'s contained within #sites
If you serve a form_for form with a an object retrieved from the database or a 'new record' object like Site.new, it will automatically differentiate and modify the route etc accordingly between the create and the update action.
The site object contained in the #sites block was not recognizable by the form_for. So a quick re factor to request an edit from via ajax, and provide the form with the instance variable created by the edit action (#site = Site.find(params[:id]) ) was recognizable by the form_for helper and meant that the submit action, accordingly adjusted to the correct route.

undefined method `model_name' for NilClass:Class on view

My code is working on local server but it's not working on the production server. I can't figure it out what I'm doing wrong.Please help me.
This is where I'm getting error in my partial:
<%=form_for #shiftchange, :url => { :controller=>"schedulers" ,:action => "shift_change" },:validate=>true , :method => :post do |f|%>
<%= f.label :from_date , "From Date " %>
<%= f.text_field :from_date ,:class =>'datepicker' %>
<% end %>
To load the partial,this is what I'm doing this:-
<%= render "schedule_shift" %>
In the controller I have this:
#shiftchange = Shiftchange.new
If the form is included for multiple actions (pages) you need to set #shiftchange to something or the form_for directive will fail (it won't have anything to create a form for).
A much better approach (and the Rails default) is to have separate views for each action, each including only the elements required by that action, and loaded into your application.html.erb layout with <%= yield %>. So you would have a app/views/shiftchanges/new.html.erb view which has the form in it. You never very rarely need to define any load paths in Rails, they are all derived from the model, controller and action names - or from your routes.rb. This is a core part of the convention over configurationhttp://en.wikipedia.org/wiki/Convention_over_configuration paradigm which runs so deep in Rails.
If you do need to have a form for creating a new object on every page (often used for ShiftchangeSessions for example), you can rewrite your form_for so that it doesn't depend on an object being present:
form_for(Shiftchange.new)
or if you need to force it to post to the create method
form_for(Shiftchange.new, :url => { :action => "create" })
You can read more about resource driven form_for in the Ruby On Rails API docs http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#method-i-form_for-label-Resource-oriented+style.
Change
form_for #shiftchange
for this
form_for :shiftchange
i guess it works

Still No Luck With Forms

I have asked a similar question but I had no luck in finding an answer. I think I provided too much information, so this time it's going to be short and sweet.
Description
I have a reply form I want to render inside a paginated item. The paginated item is defined as #share_item therefore when I write codes inside that partial they will look like this:
<% if registered_member?(share_item.user) %>
Problem
The problem occurs when I want to render the form partial. With some code omitted, it looks something
<%= form_for(#blob) do |f| %>
<%= f.text_area :content, rows: "2", placeholder: "Reply to ##{share_item.user.name}" %>
#blob is the main model controller where everything is defined, where #share_item is a paginated item of #blob. I get the error share_item is an undefined model or method.
What Works
If I add the form's html directly inside the partial, I can add something like this:
<textarea id="blob_content" name="blob[content]" placeholder="Reply to #<%= share_item.user.name %>">#<%= share_item.user.name %> </textarea>
and I will receive no errors.
What I want
I want to be able to render the form rather than adding the form directly inside the partial (it's annoying, uncanny, and space-consuming). I have other codes that are similar, and I am basically just looking for a solution on how I can get the same results from rendering a form as adding the HTML to the partial under circumstances as these.
Try:
<%= form_for(#blob) do |f| %>
<%= f.text_area :content, rows: "2", placeholder: "Reply to #<%= #share_item.user.name%>" %>

Rails generic form for with Braintree form builder

I have a similar form like this:
<%= form_for :customer,
:params => #result && #result.params[:customer],
:errors => #result && #result.errors.for(:customer),
:builder => BraintreeHelper::BraintreeFormBuilder,
:url => Braintree::TransparentRedirect.url,
:html => { :autocomplete => "off"} do |f| -%>
First name: <%= f.text_field :first_name , :value => "John"%><br />
<%= f.submit %>
<% end %>
When I try to add the value to that text field is not letting me. However, if I take the builder from the form_for tag, I am able to do so. Anyone has had experience with Braintree form builder?
Siwei Shen is on the right track...
Form builders in Rails help you tie your forms to models in your application and generate values, names, and IDs for your form elements that correspond to model attributes. They're tremendously helpful, but sometimes it's necessary to override what they give you.
The example form builder you found in the Braintree Rails example app provides some things that are helpful since TR forms post directly to our gateway rather than your app and gives your form fields attribute names and default values that Braintree's gateway will expect as well as helps to populate validation errors which are returned to your app.
In this particular case, the form builder is ultimately merging the values for the inputs (including :first_name) with either the existing value (if any) or nil (over-writing what you've passed in.
https://github.com/braintree/braintree_ruby_examples/blob/master/rails_tr_checkout/app/helpers/braintree_form_builder.rb#L22
Feel free to modify the form builder to suit your needs!
when using "form_for", the default value is the form-object's value. e.g.
#person = Person.new(:first_name => "Jack")
<%= form_for :person do |f| %>
<%= f.text_field :first_name %>
<% end %>
and you will get a text input with the default value "Jack" :
<input type="text" value="Jack"/>
so, when using f.text_field, you are not able to set the default value, unless you change the form-object ( here is the #person).
If you want to set the default value, I suggest you either use "form_tag", which does not have "form object", or use jQuery to implement the default value stuff.

Resources