Rails simple loop conditions - loops

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| %>

Related

backbone js print variable value in view which is an html

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.

Access javascript built-ins inside an angularjs expression

I'm a beginner at Angularjs and I would like to know how can I use javascript built-ins like Date or Array in expressions. It seems weird to add them like this $scope.Array = Array in every controller.
Edit: In response to your comment below, I would just add a little change to the way you have done it as I personally like to remove as much logic as possible from the view.
JavaScript
$scope.numItems = 5;
$scope.range = Array($scope.numItems);
HTML
<tr ng-repeat="i in range track by $index">
<td align="center">{{$index}}</td>

How do I use a variable passed into an Undescore template in a function in that template?

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 .

Rails check_box without an object?

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...

rails 3: how to output the content of a array

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.

Resources