Rails - Table column width is being affected by select box - html

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

Related

How to update chart (Chartkick) on a page after filtering?

I have a problem. I have made some filters using ransack and they work fine, but I have no idea how to update the pie chart that I have implemented under the table that I filter. The chart is made using Chartkick. I am fairly new to ror and programing in general. Here is my index file:
<h1>Lista Przychodów</h1>
<h2><%= link_to 'Nowy Przychód', new_income_path %></h2>
<%= search_form_for #q do |f| %>
<div class="field">
<%= f.label :title_cont, "Tytuł zawiera" %>
<%= f.search_field :title_cont %>
</div>
<div class="field">
<%= f.label :text_cont, "Opis zawiera" %>
<%= f.search_field :text_cont %>
</div>
<div class="field">
<%= f.label :category_name_cont, "Kategoria zawiera" %>
<%= f.search_field :category_name_cont %>
</div>
<div class="field">
<%= f.label :amount_gteq, "Kwota pomiędzy" %>
<%= f.search_field :amount_gteq %>
<%= f.label :amount_lteq, "a" %>
<%= f.search_field :amount_lteq %>
</div>
<%= f.submit "Szukaj" %>
<div class="button">
<%= link_to 'Wyczyść filtry', request.path, class:"cancel-button" %>
</div>
<% end %>
<div style="overflow-x:auto;">
<table class="center">
<tr>
<th><%= sort_link(#q, :title, 'Tytuł', default_order: :desc) %></th>
<th><%= sort_link(#q, :text, 'Opis') %></th>
<th><%= sort_link(#q, :amount, 'Kwota') %></th>
<th><%= sort_link(#q, :category_id, 'Kategoria') %></th>
<th colspan="3"></th>
</tr>
<% #incomes.each do |income| %>
<tr>
<td><%= income.title %></td>
<td><%= income.text %></td>
<td><%= number_to_currency(income.amount, :unit => "zł", :format => "%n %u") %></td>
<td><%= Category.find(income.category_id).name %></td>
<td><%= link_to 'Pokaż', income_path(income) %></td>
<td><%= link_to 'Edytuj', edit_income_path(income) %></td>
<td><%= link_to 'Usuń', income_path(income),
method: :delete,
data: { confirm: 'Czy na pewno chcesz usunąć ten wpis?' } %></td>
</tr>
<% end %>
</table>
</div>
<%= pie_chart Income.joins(:category).group(:name).sum(:amount),suffix: " zł", thousands: ",", messages: {empty: "Nie ma wpisów"}, download: {filename: "wykres"}, title: "Podział przychodów na kategorie" %>
<footer><%= link_to 'Strona główna', controller: 'welcome' %></footer>
And my controller file if it is needed:
class IncomesController < ApplicationController
helper_method :sort_column, :sort_direction
http_basic_authenticate_with name: "admin", password: "admin", except: [:index, :show]
def index
#q = Income.search(params[:q])
#incomes = #q.result.includes(:category)
end
def show
#income = Income.find(params[:id])
end
def new
#income = Income.new
#categories = Category.all.map { |c| [c.name, c.id]}
end
def edit
#income = Income.find(params[:id])
#categories = Category.all.map { |c| [c.name, c.id]}
end
def create
#income = Income.new(income_params)
#categories = Category.all.map { |c| [c.name, c.id]}
if #income.save
redirect_to #income
else
render 'new'
end
end
def update
#income = Income.find(params[:id])
if #income.update(income_params)
redirect_to #income
else
render 'edit'
end
end
def destroy
#income = Income.find(params[:id])
#income.destroy
redirect_to incomes_path
end
private
def income_params
params.require(:income).permit(:title, :text, :amount, :category_id)
end
def sort_column
params[:sort] || "title"
end
def sort_direction
params[:direction] || "asc"
end
end
I already looked on the internet for a similar problem but didn't find anything. I would appreciate some help with that or at lest some directions what to do. I got really hooked on ror and would like to learn more about it, that's why I came here for help. Thanks in advance for answers.
If you mean the filters are applied on the table but not on the chart, this is because you are making another query for the chart only, in this line:
<%= pie_chart Income.joins(:category).group(:name).sum(:amount),suffix: " zł", thousands: ",", messages: {empty: "Nie ma wpisów"}, download: {filename: "wykres"}, title: "Podział przychodów na kategorie" %>
You should use the same instance variable that has the filtered data:
#incomes
So it should be something like:
<%= pie_chart #incomes.sum(:amount), suffix: " zł", thousands: ",", messages: { empty: "Nie ma wpisów" }, download: { filename: "wykres" }, title: "Podział przychodów na kategorie" %>

How to add a class to a select dropdown loop using ERB

I have the following loop to create a select dropdown using ERB. It is working correctly.
<%= f.select(:player_id) do %>
<% #players.each do |p| %>
<%= content_tag(:option, "#{p.first_name} #{p.last_name}", value: p.id) %>
<% end %>
<% end %>
My question is how do I add a class to the select element?
I have tried the following:
<%= f.select(:player_id), class: "form-control" do %>
<% #players.each do |p| %>
<%= content_tag(:option, "#{p.first_name} #{p.last_name}", value: p.id) %>
<% end %>
<% end %>
and
<%= f.select(:player_id), { class: "form-control" } do %>
<% #players.each do |p| %>
<%= content_tag(:option, "#{p.first_name} #{p.last_name}", value: p.id) %>
<% end %>
<% end %>
I have seen questions similar to this, but none that use a loop like the example above.
This should work for you.
<%= f.select :player_id, options_for_select( #players.collect { |player| ["#{player.first_name} #{player.last_name}", player.id] } ), class: 'form-control' %>
This is what I ended up doing:
<%= f.select :player_id, options_for_select( #players.collect { |player| ["#{player.first_name} #{player.last_name}", player.id] } ), {}, { class: 'form-control' } %>
I think problem with your code is f.select(:player_id).
You are calling the select method with only one parameter :player_id. So you can pass other options like this.
<%= f.select :player_id, class: "form-control" do %>
<% #players.each do |p| %>
<%= content_tag(:option, "#{p.first_name} #{p.last_name}", value: p.id) %>
<% end %>
<% end %>

How do I nest models using a form_tag in Rails 4?

I've got a form to add SaleQualifiers to my app - which works fine when I'm using:
<%= form_for(#sale_qualifier, :html => {role: :form, 'data-model' => 'sale_qualifier'}, remote: true) do |f| %>
The problem with form_for is that I want to put the form inline within a table row in my view - so as a method to get around that I'm now using:
<%= form_tag('/sale_qualifiers', method: :post, remote: true) do -%>
<%= fields_for :sale_qualifier do |ff| %>
This is working fine for most of the fields I need to generate, but I've got a nested attribute field for Answer (Answer belongs_to SaleQualifier). This is not generating the right field names in the view, and as a result when I go to save the object this way I don't capture the answer_attributes.
Here's the full working form using form_for:
<div class="panel panel-default">
<%= form_for(#sale_qualifier, :html => {role: :form, 'data-model' => 'sale_qualifier'}, remote: true) do |f| %>
<% if #sale_qualifier.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#sale_qualifier.errors.count, "error") %> prohibited this answer from being saved:</h2>
<ul>
<% #sale_qualifier.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="panel-body">
<div class="col-sm-6">
<h2><%= #question.question_text %></h2>
<% unless #question.id == 1 %>
<p><%= link_to('Back', edit_sale_qualifier_path(id: #prior_sale_qualifier), data: { disable_with: "Loading..." }, :remote => true) %></p>
<% end %>
</div>
<div class="col-sm-6">
<div class="form-group">
<%= f.hidden_field :sales_opportunity_id, :value => #sales_opportunity.id %>
</div>
<div class="form-group">
<%= f.hidden_field :question_id, :value => #question.id %>
</div>
<% unless #question.id == 1 %>
<div class="form-group">
<%= f.hidden_field :prior_question_id, :value => #prior_question_id %>
</div>
<% end %>
<%= f.fields_for :answer do |answer| %>
<div class="form-group">
<% if #question.answer_type == 'Text Field' %>
<%= answer.text_area :answer_text, :placeholder => "Enter your answer", :class => "form-control"%>
<% end %>
<% if #question.answer_type == 'Datetime' %>
<div class='input-group date' id='datetimepicker' data-date-format="YY.MM.DD">
<%= answer.text_field :answer_text, class: "form-control", data: { date_format: 'YYYY/MM/DD' }, :placeholder => "YYYY/MM/DD" %>
<span class="input-group-addon">
<span class="glyphicon glyphicon-calendar"></span>
</span>
</div>
<% end %>
<% if #question.answer_type == 'Boolean' %>
<%= answer.select :answer_text, [['Yes', true], ['No', false]] %>
<% end %>
<% if #question.answer_type == 'Update' || #question.answer_type == 'Result' %>
<%= answer.hidden_field :answer_text, :value => "Updated" %>
<% end %>
<span class="warning-block"></span>
<span class="help-block"></span>
</div>
<% end %>
<% if #question.answer_type == 'Update' || #question.answer_type == 'Result' %>
<div class="actions">
<%= f.submit "Done", class: "btn btn-large btn-success", data: { disable_with: "Submitting..." }, autocomplete: 'off' %>
</div>
<% else %>
<div class="actions">
<%= f.submit "Submit", class: "btn btn-large btn-success", data: { disable_with: "Submitting..." }, autocomplete: 'off' %>
</div>
<% end %>
<% end %>
</div>
</div>
</div>
Here's the code which does not work using form_tag:
<%= form_tag('/sale_qualifiers', method: :post, remote: true) do -%>
<%= fields_for :sale_qualifier do |ff| %>
<%= ff.hidden_field :sales_opportunity_id, :value => #sales_opportunity.id %>
<%= ff.hidden_field :question_id, :value => #question.id %>
<tr>
<td><%= #question.question_text %></td>
<td>
<%= ff.fields_for :answer do |answer| %>
<% if #question.answer_type == 'Text Field' %>
<%= answer.text_area :answer_text%>
<% end %>
<% if #question.answer_type == 'Datetime' %>
<div class='input-group date' id='datetimepicker' data-date-format="YY.MM.DD">
<%= answer.text_field :answer_text, class: "form-control", data: { date_format: 'YYYY/MM/DD' }, :placeholder => "YYYY/MM/DD" %>
<span class="input-group-addon">
<span class="glyphicon glyphicon-calendar"></span>
</span>
</div>
<% end %>
<% if #question.answer_type == 'Boolean' %>
<%= answer.select :answer_text, [['Yes', true], ['No', false]] %>
<% end %>
<% end %>
</td>
<td>
<%= ff.submit "Submit", class: "btn btn-large btn-success", data: { disable_with: "Submitting..." }, autocomplete: 'off' %>
</td>
</tr>
<% end %>
<% end %>
For completeness, the issue I'm having is that the generated html for the working code creates the following field:
textarea id="sale_qualifier_answer_attributes_answer_text" name="sale_qualifier[answer_attributes][answer_text]"
The broken code creates the following html:
Textarea id="sale_qualifier_answer_answer_text" name="sale_qualifier[answer][answer_text]"
So how can I get the html output to show "sale_qualifier[answer_attributes][answer_text]" rather than "sale_qualifier[answer][answer_text]" in this instance using form_tag?
In multiple nested forms the fields_for tag will be called against
right parent and attributes conventions should be followed right otherwise
it renders the form attributes wrong and results in errors.
<%= form_tag('/sale_qualifiers', method: :post, remote: true) do -%>
<%= fields_for :sale_qualifier do |ff| %>
<%= ff.fields_for :answer_attributes do |answer| %>
Above flow will be the right one & will generate attributes as they should be.

Make a specific row bold based on query

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>

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>