Make a specific row bold based on query - html

I have a .html.erb file which is used to display a form on a website. (Redmine)
I need to make a specific row bold. When the tracker == Anomaly, the row must be bold.
<% if query.columns == Anomaly>
make the text bold
but I think I have the wrong logic. How can I implement my idea?
I know what the ruby rows are processed in the following code. But I have no idea how to make a row bold based on a query.
the current code
<style>
.bolding {font-weight: bold;}
</style>
<tr id="issue-<%= issue.id %>" class="bolding hascontextmenu
<%= cycle('odd', 'even') %>
<%= issue.css_classes %> <%= level > 0 ? "idnt idnt-#{level}" : nil %>">
<td class="checkbox hide-when-print"><%= check_box_tag("ids[]", issue.id, false, :id => nil) %></td>
<td class="id"><%= link_to issue.id, :controller => 'issues', :action => 'show', :id => issue %></td>
<% query.columns.each do |column| %><%= content_tag 'td', column_content(column, issue), :class => column.css_classes %><% end %>
</tr>

You can avoid all of the confusion if you word your question clearly, and provide all relevant details. As #iain mentioned, query does not come from Rails, it comes from Redmine. You should have mentioned you use Redmine in your question.
That said, it looks like query works with/from this class.
Each column in query.columns has a name attribute and a value() method (among other things).
You can check for one of these columns in the query.columns collection having a value "Anomoly" and name "Tracker" like this.
query.columns.any? { |c| c.name.downcase == "tracker" && c.value(issue) == "Anomoly" }
You need to get the return value of this to conditionally apply the bolding class to your table row.
<%= apply_bolding = query.columns.any? { |c| c.name.downcase == "tracker" && c.value(issue) == "Anomoly" } %>
<tr id="issue-<%= issue.id %>" class="<%= apply_bolding ? "bolding" : nil %> hascontextmenu <%= cycle('odd', 'even') %>

This was the right way to do it
<tr id="issue-<%= issue.id %>" class="hascontextmenu <%= cycle('odd', 'even') %> <%= issue.css_classes %> <%= level > 0 ? "idnt idnt-#{level}" : nil %>"
<% if issue.tracker_id == 12 %> style="font-weight:bold" <% end %>
>
<td class="checkbox hide-when-print"><%= check_box_tag("ids[]", issue.id, false, :id => nil) %></td>
<td class="id"><%= link_to issue.id, :controller => 'issues', :action => 'show', :id => issue %></td>
<% query.columns.each do |column| %><%= content_tag 'td', column_content(column, issue), :class => column.css_classes %><% end %>
</tr>

Related

Search with Ruby on Rails

I am currently learning Ruby on Rails, however, I have run into a slight issue, I am unable to create a text field from which to search. I so far have a working DB and web page (albeit an ugly one), however, I am having a problem with this simple function, here is my code so far:
<h1>Listing articles</h1>
<%= link_to 'New article', new_article_path %>
<table>
<tr>
<th>Title</th>
<th>Text</th>
<th colspan="3"></th>
</tr>
<% #articles.each do |article| %>
<tr>
<td><%= article.title %></td>
<td><%= article.text %></td>
<td><%= link_to 'Show', article_path(article) %></td>
<td><%= link_to 'Edit', edit_article_path(article) %></td>
<td><%= link_to 'Destroy', article_path(article),
method: :delete,
data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
<strong>Search Title:</strong>
text_field_tag(:id)
<%= #article = Article.find(params[:id]) %>
<% end %>
</table>
error
/Users/jake/Documents/internships/rails/search/app/views/articles/index.html.erb:31: syntax error, unexpected keyword_ensure, expecting end-of-input
just for the web page, any help would be strongly appreciated.
Thanks in advance!
Try looking through this code:
In your html:
<% form_tag projects_path, :method => 'get' do %>
<p>
<%= text_field_tag :search, params[:search] %>
<%= submit_tag "Search", :name => nil %>
</p>
<% end %>
In your controller:
def index
#projects = Project.search(params[:search])
end
and in the model
def self.search(search)
if search
find(:all, :conditions => ['name LIKE ?', "%#{search}%"])
else
find(:all)
end
end
Here is the link
Also as I'm doing in my project, you can create separate action in your index/welcome controller called search:
def search
end
Then configure route in your routes.rb like so(not sure exactly this will work)
get 'welcome/' => 'welcom#search', as: 'search'
And then in view create a form:
<%= form_tag search_path, method: 'get', remote: true, id: "garage-search" %>
<%= text_field_tag :search %>
<% end %>
This will send search request by AJAX to your search controller. There you can process it as needed.
Also try look here. Very clearly explained..

Add pagination to existing Rails site?

So I have a Rails app with posts and categories, and as I'm having the website tested out by friends I realized I forgot to add any pagination to category pages, so once a lot of listings are added the page just keeps getting longer and longer which is obviously not ideal.
Here's the code on each category page that renders the listings:
<% notsold = #category.products.where("sold_value = false").order("created_at DESC") %>
<% notsold.each do |f| %>
<% if User.find(f.user_id).school == current_user.school %>
<div class="listing">
<%= image_tag User.find(f.user_id).avatar.url(:thumb).sub('http://s3.amazonaws.com/anymarket/','http://anymarket.s3.amazonaws.com/'), :id => "categories_profile_picture", :width => "30", :height => "30" %><h4><%= link_to f.name, view_item_path(f.id) %></h4><% if f.photo? %><span id="camera-icon-preview" class="glyphicon glyphicon-camera preview_toggle" data-id="<%= f.id %>"></span><% else %><% end %><p><%= f.description[0..60].gsub(/\s\w+\s*$/, '...') %></p><p class="price"><%= number_to_currency(f.decimal_price, precision: 2) %></p>
</div>
<% else %>
<% end %>
<% end %>
Div#listing is each individual listing.
The problem is I don't know how to preserve this code and add pagination. I'm looking for some advice on how to do this.
You can use the will_paginate gem
example from page:
In your controller like this:
#posts = Post.paginate(:page => params[:page])
more examples:
## perform a paginated query:
#posts = Post.paginate(:page => params[:page])
# or, use an explicit "per page" limit:
Post.paginate(:page => params[:page], :per_page => 30)
## render page links in the view:
<%= will_paginate #posts %>
Not sure but this should work out for you:
<% notsold = #category.products.where("sold_value = false").order("created_at DESC").paginate(:page => params[:page], :per_page => 30) %>
<% notsold.each do |f| %>
<% if User.find(f.user_id).school == current_user.school %>
<div class="listing">
<%= image_tag User.find(f.user_id).avatar.url(:thumb).sub('http://s3.amazonaws.com/anymarket/','http://anymarket.s3.amazonaws.com/'), :id => "categories_profile_picture", :width => "30", :height => "30" %><h4><%= link_to f.name, view_item_path(f.id) %></h4><% if f.photo? %><span id="camera-icon-preview" class="glyphicon glyphicon-camera preview_toggle" data-id="<%= f.id %>"></span><% else %><% end %><p><%= f.description[0..60].gsub(/\s\w+\s*$/, '...') %></p><p class="price"><%= number_to_currency(f.decimal_price, precision: 2) %></p>
</div>
<% else %>
<% end %>
<% end %>

Ruby on Rails how to include a form in an index view

I've been running my head into a wall for hours now and I can't make this work. I'm trying to add a bunch of check boxes to verify items on my index page. I found this old rails cast that does exactly what I want to do, but I've run into a problem. Anything enclosed inside my form is removed from the index page, like just gone poof. Here's the code from the index view.
<% form_tag verify_products_path, :method => :put do %>
<% #products_unverified.each do |products| %>
<% if product.deleted != 'true' %>
<tr data-link="<%= product_path(product) %>">
<td><%= product.name %></td>
<td><%= product.description %></td>
</tr>
<% end %>
<% end %>
<%= submit_tag "Mark as Verified" %>
<% end %>
</tbody>
Here's the routes stuff
resources :products do
put :verify, :on => :collection
end
and the controller just has a dummy method for now.
def verify
end
Any clue as to why the index view blanks out when the form is introduced? Any help is much appreciated.
You forgot the = before form_tag:
<%= form_tag verify_products_path, :method => :put do %>
put an = in front of form_tag
instead of
<% form_tag verify_products_path, :method => :put do %>
try
<%= form_tag verify_products_path, :method => :put do %>

Rails - Table column width is being affected by select box

I have a table that seems to adapt to the longest text string in a given column. I have select tags in each row which seem to be messing up the display. There is an extra column being added to the table and the border on the right is not displaying. If I shrink the size of the select tags, the table is formatted correctly, but I'd like to just have the table conform to the size of the largest select tags and largest buttons in the next column. Thanks. Here's the code:
<table class="table table-bordered table-condensed">
<thead>
<tr>
<th>Position</th>
<th>Allocation</th>
<th>Action</th>
</tr>
</thead>
<% position_map = #cabinet.cabinet_position_map %>
<% #cabinet.cabinet_type.usize.downto(1) do |i| %>
<tr>
<td>Slot: <%= i %></td>
<% if position_map[i].nil? %>
<% cabinet_device_selection = #cabinet.devices_to_fit(position_map, i) %>
<% #selection_list = [ "Power", "I/O", "Interface", "Unusable", "Other", "Missing", "Reserve" ] %>
<% cabinet_device_selection.each do |device| %>
<% #selection_list << device.name %>
<% end %>
<% if position_map[i].nil? %>
<%= form_tag( {:controller => :cabinets, :action => :update_device_position, :position => i , :id => #cabinet.id } , { :method => 'get', :class => "form-search" }) do %>
<td>
<%= select_tag :position_name, options_for_select(#selection_list) %>
</td>
<td>
<%= hidden_field_tag 'position', i %>
<%= submit_tag "Add" , :class => "btn" %>
</td>
<% end %>
<% end %>
<% else %>
<% position_map[i].each do |cabinet_item| %>
<td>
<%= cabinet_item.name %>
</td>
<td>
<%= link_to "Remove", { :controller => :cabinets, :action => :remove_cabinet_position, :id => #cabinet.id, :position => i}, :class => "btn btn-danger" %>
</td>
<% end %>
<% end %>
</tr>
<% end %>
</table>
And here's what the output looks like:
It seems you are using Bootstrap.
So, to change the width of select_tag you can simply do.
<%= select_tag :position_name, options_for_select(#selection_list), :class => "span1" %>
Or, if you want to set custom width, add a class in your css.
.option-small {
width: 100px !important;
}
And then use
<%= select_tag :position_name, options_for_select(#selection_list), :class => "option-small" %>

Hiding html elements

I'm attempting to hide the links for edit and destroy actions in the view unless the user has logged in using http basic auth. I've attached my files below. Thanks
View https://gist.github.com/1272716
Controller https://gist.github.com/1272712
You need to save/store the authenticate result, and then render conditionally in your view.
Controller:
protected
def authenticate
#authenticated = authenticate_or_request_with_http_basic do |user, password|
user == "x4556d4s" && password == "fd55sas64x"
end
end
View:
<%= link_to "New Link", :controller => :links, :action => :new %>
<table>
<% #links.each do |link| %>
<tr>
<td> <%= link_to '+', up_link_url(link), :method => :put %> <%= link.points %> <%= link_to '-', down_link_url(link), :method => :put %> </td>
<td> <%= link.title %> </td>
<% if #authenticated %>
<td><%= link_to 'Destroy', link, :confirm => 'Are you sure?', :method => :delete %></td>
<td><%= link_to 'Edit', edit_link_path(link) %></td>
<% end %>
</tr>
<% end %>
</table>