Im updating to rails 3 and experience the problem of writing the content of a array to a html view. If I just place the array like:
<%= array %>
It gets now outputed as:
["...","...","..."]
with rails 2 it was just the content which got printed...
Any Ideas?
Markus
<%= array.join ' ' %>
You can try using array#to_sentence method:
<%= array.to_sentence %>
Or just a join:
<%= array.join(", ") %>
Depends on how you want the output to look.
If it's for debugging purposes, why not try:
<%= debug array %>
if you need to present the array in a specific way, you're better off iterating the array and presenting each element with a repeatable block or a partial.
Related
I am very new to backbone.js and learning it. I managed to pass my variables to the view and they are being displayed fine. What I am struggling with is what if the variable being passed to the view is an html that looks like this
<p>I am an html print me on view</p> now suppose this variable is called description so inside the view if I do
<%-description %>
This actually displays the result in html as you can see in the following screenshot. So my question is how can i print a variable on view which is an html?
Any help will be greatly appreciated.
Have a look at _.template doc, it says
Template functions can both interpolate values, using <%= … %> [...] If you wish to interpolate a value, and have it be HTML-escaped, use <%- … %>
Use <%= description %> to insert the raw, unescaped HTML.
I am using Underscore to create a template. I pass the models of a collection to the template, in this case a list of 'phone' objects.
Inside of the template, I define a function that, when called, creates a table row that includes a select that contains the phone numbers that can be chosen.
I have tried 'var phone = <%= phones %> inside the function. Didn't work.
Say the function is called 'create_select'. I have tried 'create_select(<%=phones %>) {}'. That didn't work either.
There might be other ways of doing this (for example, a create_select event handled in the Backbone View linked to this template) but I would like to understand, if this is not possible, why not.
You have to call it inside your script let tag .For accessing the value and pass it to a function.
Sample js fiddle for function inside a template
<% var x = people; disp(x); %>
<% function disp(x) { %>
<% _.each(x, function(name) { %>
<li><%= name %></li>
<% }); %>
<% } %>
In the sample code I am passing a json object and inside the template I have a function called disp(). I'm passing the value to the function and parsing it using _.each() function .
How can I do something like ActionVIew::Helpers::FormHelper#check_box, but without an object to hang it on?
I'm building a user profile editor. Some of my fields are ordinary booleans, and check_box is great with them:
app/views/users/edit.html.erb:
<%= f.label :admin %>
<%= check_box :user, :admin %>
produces the expected extra hidden field
<label for="user_admin">Admin</label>:
<input name="user[admin]" type="hidden" value="0" />
<input id="user_admin" name="user[admin]" type="checkbox" value="1" />
and posts the setting as expected.
But my users also have a password, and the behavior I want isn't actually to set or clear a boolean field, but rather to trigger an email-mediated reset protocol. I'd like it to look the same as the simple check_box :user, :admin case. User objects don't have a "reset_password" field, so this is just a syntax error:
<%= f.label :reset_password %>
<%= check_box :user, :reset_password %>
EDIT: I guess I could make an actual db column / object attribute named reset_password, which is always false, but when the controller sees an attempt to set it, it instead launches the reset protocol. But an always-false, read-only attribute seems a tad silly, doesn't it?
If it's not part of the user profile data, why make it a checkbox? It could just be a link instead of a checkbox, either on the user edit screen or on the user profile view. If you really want it as a checkbox in the form, you could also just make the checkbox part of a subform that would get submitted at the same time as the main form, using a javascript call, and hiding the submit button...
Absolute rails newb here.
For my index view, how would I change the loop conditions for <% #complaints.each do |complaint| %> to do something like where #complaint.user_id == current_user?
It's best to do this in the controller, but in any case in the view it's something like:
<% #complaints.where(user_id: current_user).each do |complaint| %>
I have a member form in which there is a select box for choosing a group of this member.
I'm using Backbone & Backbone.Marionette. The member form is bound to a MemberModel. Data for the select box comes from a GroupCollection. MemberModel contains only a group_id, not any reference to a GroupCollection.
I'm now generating this select box manually (fetch GroupCollection then add to select box by using Javascript).
I would like to know if there is a better way to do it in Backbone.Marionette. I thought it could be done with ListView and ItemView but I couldn't know how to add the select box to this Member form because this form is not a Layout.
There's many different ways that this could be done, and a CollectionView could be used. You would have to set the CollectionView's tagName to "select" and then the itemView used with the CollectionView would have a tagName set to "option".
The easier idea might be to use an ItemView on it's own, with your collection as the data source. You can use a collection with an ItemView no problem, you just have to do the loop in your template.
Using underscore.js templates:
<script type='text/template' id='some-template'>
<select>
<%= _.each(items, function(item){ %>
<option value="<%= item.value %>"><%= item.name %>
<%= }) %>
</select>
</script>
Marionette.ItemView.extend({
template: "#some-template"
// ...
})
I wrote about these two basic options in more detail, here: http://lostechies.com/derickbailey/2011/10/11/backbone-js-getting-the-model-for-a-clicked-element/
Hope that helps.