ASP.NET MVC View Code Formatting - braces <% } %> - visual-studio

Does anyone know how to configure visual studio to correctly format code segments within a View
This annoys the crap outta me!:
<select>
<%
foreach(Height height in ViewData.Model.Heights)
{%>
<option value="<%=height.ID %>"><%=height.Value%></option>
<%
}%>
</select>
It should be like this:
<% foreach(Height height in ViewData.Model.Heights) { %>
<option value="<%=height.ID %>"><%=height.Value%></option>
<% } %>
</select>
I can fix it manually but whenever I reformat, or change some of the code close to the braces it screws up again!

After the auto formatting takes place hit cntrl-z, it will undo the auto-formatting only and not rearrange your code.

Related

DocPad: Empty or Missing YAML Elements

I have the following in one of my Layout files...
<% for link in #document.nextlink[0..0]: %>
<% if (link.name): %>
<ul class="actions">
<li><%- "#{link.helptext}" %></li>
</ul>
<% end %>
<% end %>
This works great. Unless, nextlink is not in my YAML at all. But that's what I want. Sometimes I want to have that button called Next and sometimes I do not.
if nextlink is not in my YAML, the DocPad compiler throws an error.
Any help would be much appreciated.

trying to populate dropdown, error in if else condition ruby

This is the below code that I had written for populating a dropdown.
<html>
<select id = 'status_update' >
<% array = [{"status_name"=>"Submitted", "reachable"=>false, "transition_name"=>""},
{"status_name"=>"Replied", "reachable"=>true, "transition_name"=>"Reply"},
{"status_name"=>"Answered", "reachable"=>false, "transition_name"=>""},
{"status_name"=>"Closed", "reachable"=>false, "transition_name"=>""},
{"status_name"=>"Canceled", "reachable"=>true, "transition_name"=>"Cancel"}]
array.each { |x|
x.each do |key, value|
%>
<option value = "<%= #{x['transition_name']} %>"
disabled = "<%= if ((#{x['reachable']}) == 'false')
return 'disabled'
else
return ''
end %>" ><%= "#{x['status_name']}" %></option>
<% end %>
<% } %>
</select>
</html>
In the above code, array is collection of hashmaps and the keys in hashmaps remains the same, 3 keys with different values in each set. Now, I am trying to populate each hashmap, with the values of their respective keys, in the dropdown. When I try so, I am getting error
ERB syntax error:dropdown:23: syntax error, unexpected kELSE
else
Might be simple, but not able to get the correct way of approach to get the hashmaps in the dropdown. Can anyone of you kindly let me know how to proceed please ?
The second loop in not necessary. This should work:
<% array.each do |x| %>
<option value="<%= x['transition_name'] %>" disabled="<%= x['reachable'] ? '' : 'disabled' %>"><%= x['status_name'] %></option>
<% end %>
Rather than setting disabled="" it's better to omit the attribute:
<option value="<%= x['transition_name'] %>" <%= 'disabled="disabled"' unless x['reachable'] %>"><%= x['status_name'] %></option>
Or, if this is too much in one line:
<% if x['reachable'] %>
<option value="<%= x['transition_name'] %>"><%= x['status_name'] %></option>
<% else %>
<option value="<%= x['transition_name'] %>" disabled="disabled"><%= x['status_name'] %></option>
<% end %>

how to filter an <option> value in ruby for quotes

Have a do loop in HTML supporting a Ruby app:
<% #list.each do |object| %>
<option value="<%= object['name'] %>"><%= object['name'] %></option>
<% end %>
Would like to be able to filter the <option value="<%= object['name'] %>"> item for quotes to preserve the final HTML -- is there an easy way to do this?
If this is a Rails app, then I'd suggest using one of the built-in helpers, like so:
options_for_select(#list.map{ |object| [object['name'], object['name']] })
If not, then maybe just gsub:
<option value="<%= object['name'].gsub("\"","") %>">
In pure Ruby, you can use CGI::escapeHTML, like this:
<% #list.each do |object| %>
<% escaped_value = CGI::escapeHTML(object['name']) %>
<option value="<%= escaped_value %>"><%= escaped_value %></option>
<% end %>
You will have to do a require 'cgi', to use this.
If you are using Rails, you are better off using options_for_select, or use the select_tag or select according to your need, as the other answer specifies

How can I hide a search button and show a spinner while my AJAX partial loads in rails 3.1?

I have a partial which returns results from the Amazon-ecs API (searches for books). It takes about 2.5 seconds to return values, so I want to add a spinner (and ideally hide the search button) but I've had a tough time getting it to work.
I already have javascript refreshing the partial with the results. I'd just like the button to give me a spinner (and hide the search button) until the partial finishes reloading.
Here is my main view (index.html.erb) which renders a partial of search results, each of which is added temporarily as an object in AmazonItems:
<%= form_tag amazon_items_path, :method => :get, :remote => true do %>
<p>
<%= text_field_tag :query, params[:query] %>
<span id="spinner" style="display:none">
<%= image_tag 'ajax-loader.gif' %>
</span>
<span id="search_button">
<%= submit_tag "Search" %>
</span>
</p>
<% end %>
<h1>Searching Amazon</h1>
<div id="amazon_returned">
<%= render 'amazon_returned' %>
</div>
My only JS code is in the update.js.erb file, which looks like:
$("#amazon_returned").html("<%=j render 'amazon_returned' %>");
I'm sure this is easy, but I can't get it work. Thanks!
EDIT: my partial "_amazon_returned.html.erb" looks like
<table>
<% for amazon_item in #amazon_items %>
<td> amazon_item.title </td>
...ETC...
<% end %>
</table>
You can hook into the before event to do this:
$(function() {
$("form").on("ajax:before", function() {
$("#spinner, #search_button").toggle();
});
});
Then, in your update.js.erb, add this again to toggle them back:
$("#spinner, #search_button").toggle();
Went with Dylan's answer, added the toggle to the update.js.erb and inserted the following into the view:
<script>
$(function() {
$("#search_button").click(function() {
$("#spinner, #search_button").toggle();
});
});
</script>
Very cool! Thanks again for the help.

Conditional navigation bar

Hello Guys I've a request for you.I've Drinks, Menus and Users routes.
I've this code in views/layout/application.html.erb:
<div>
<%= render "shared/navigation_bar"
</div>
How to make the navigation_bar to render in all the pages except in users pages?
I finally work around many methods, but I finally come up with a solution(not really stunnin', I must confess) , that works for me.
<div>
<% if controller.controller_name == "drinks" %>
<%= render "shared/navigation_bar" %>
<% elsif controller.controller_name == "menus" %>
<%= render "shared/navigation_bar" %>
<% end %>
</div>
The flow control doesn't include Users's controller, so the navigation_bar layout won't appear on User's pages.
That's it.If you guys have a better alternative just let me know.

Resources