This page is to edit the account information.
Template file,
<input type="text" id="account_name" class="form-control" placeholder="Enter Name" value="<%=account.name%>"/>
<input type="text" id="account_company_name" class="form-control" placeholder="Enter Company Name" value="<%=account.company_name%>"/>
<a id="account_next_btn" class="btn" role="button">Next</a>
view file,
events:
'click #account_next_btn': "updateAccount"
updateAccount: (e)->
e.preventDefault()
#account.save({"name": #$el.find("#account_name").val(),"company_name": #$el.find("#account_company_name").val()})
ok, this works fine. it sends the updated input form parameters.
the thing i'm curious is, there should be a better way, not setting the updated values manually like my code.
in rails backbone:scaffold it doesn't have this kind of code.
it just
#model.save()
that is all they do.
but in my code, if i just call
#account.save()
it sends the parameter, that not have been updated.
That’s because Backbone by default doesn’t include any kind of data binding library. Data binding lets you keep a model attribute synced with a form field value.
Backbone-Rails includes the simple backbone_datalink.js, which sets up a simple form binding when you create the scaffold.
There are many other binding plugins that work with Backbone, such as Backbone.ModelBinder and Rivets.js.
Related
I'm attempting to write a little script that, as part of it, would automatically complete a webform using data from a dictionary.
I've developed this script for other forms in the past with a pretty simple setup: the elements in the form are identified with a line like...
<input type="text" name="full_name" id="full_name_id" maxlength="80" value="">
<input type="text" name="title" id="title_id" maxlength="80" value="">
So, I'd navigate to www.theformsite.com/theform.php?full_name_id=David&title_id=Stuff and find those slots already filled in.
For a couple new forms I've been working with though, that doesn't seem to work: there's no response to these parameters. Are there any general things that could prevent this from working that I should check on?
I'm trying to have a dynamic set of inputs in a form, that start with just one, where you can add or remove them with add/delete buttons. Then upon submission of a form, it turns the values of the inputs into a hash then that hash into a string for storing. I really have no idea where to start. So any tips will be helpful.
If using javascript would help, I can go that route, but i'm not sure how to make the javascript and ruby talk.
Depending on your use-case, there are a few options you might want to use. Since you've tagged this with rails, I'm assuming you have access to JQuery. Here's one (very simple) example of how you might go about adding fields to the page dynamically using it:
https://jsfiddle.net/3Lyvw0jm/
If you plan on storing these fields in one of your models, you may want to take a look at implementing nested attributes.
As pretty much a common web thing (not Rails-specific), you would make the name value look like some_name[].
So instead of having multiple inputs with different names like this:
<input type='text' id='my_input_1' name='my_input_1' value='string_1' />
<input type='text' id='my_input_2' name='my_input_2' value='string_2' />
<input type='text' id='my_input_3' name='my_input_3' value='string_3' />
...where on the server you get:
params :my_input_1 # 'string_1'
params :my_input_2 # 'string_2'
params :my_input_3 # 'string_3'
You would have:
<input type='text' id='my_input_1' name='my_inputs[]' value='string_1' />
<input type='text' id='my_input_2' name='my_inputs[]' value='string_2' />
<input type='text' id='my_input_3' name='my_inputs[]' value='string_3' />
...where on the server you get:
params :my_inputs # ['string_1','string_2',string_3']
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>
Simple question... Here is an example of some razor code:
#Html.TextBoxFor(c => c.RevisedEstimate)
#Html.TextBoxFor(c => c.RevisedEstimate)
Here is how this renders:
<input data-val="true" data-val-number="The field Revised Estimate must be a number." id="RevisedEstimate" name="RevisedEstimate" type="text" value="0" />
<input id="RevisedEstimate" name="RevisedEstimate" type="text" value="0" />
The obvious question you ask is, "Why are you doing that?". The razor view is actually building client side detail-row templates that are used in KendoUI grids. There are two similar grids and we use the same viewmodel server side. We actually do provide the id element for the template so each field in each row ends up with a unique id.
Why does the second input element not have the data-val and data-val-number elements?
Off the top of my head knowing what the JS does in the background, it seems to do this to prevent conflicts. The JS looks for the elements with the data- attributes to do it's validation, along with other functions, so it could possibly pick the wrong one if there are multiple instances of it.
since we were generating HTML for use in a client side template what we did was just create a variable to hold the HTML generated by the helper, and then render out that code in the Views..
Something like:
#{
var revisedEstimateInput = Html.TextBoxFor(c => c.RevisedEstimate)
}
Then later in the view:
#(revisedEstimateInput)
...in as many places as needed. This way the validation and other metadata attributes were in place in our client templates and all the kenodUI validation worked correctly.
I am using an id value that I pass in a hidden field. When the user submits the form I need the hidden field for my update. After the update, a new value is placed in the hidden field in the model and sent back to the view. What seems so strange is the helper always uses the first value, never updates. For example, look at the following from the View:
<%: Html.Hidden("MyId",Model.MyId) %>
<%: Model.MyId %>
First time in a look at the source in the browser yields:
<input type="hidden" id="MyId" name="MyId" value="1" />
1
** submit back to controller and model updates the MyId property to 2.
Back at the browser I now find:
<input type="hidden" id="MyId" name="MyId" value="1" />
2
The very same model property has different values! The helper method is somehow grabbing it from a prior model instance or something?
Any help greatly appreciated on what I am not understanding. BTW..get the same behavior with Html.TextBox and Html.TextBoxFor.
Thanks.
That's how HTML helpers work and it's by design. When binding they will first look at the value in the GET/POST request to see if the value is present and after that in the model. If a value is found in the request they will simply ignore the value you set in the model.
Normally you are not supposed to modify the data sent in the request inside your controller action. But if anyhow you decide to do it you will need to either roll your own helper or simply:
<input type="hidden" name="MyId" value="<%= Model.MyId %>" />