I'm rails beginner.
I want to make two button that are placed in a line.
How can I put the button correctly on a line of the table?
Two buttons:
<%= button_to "empty Cart", #cart, method: :delete %>
<%= submit_tag "Delete item" %>
my view
<%= form_tag destroy_multiple_carts_path, method: :delete do %>
<table class="table">
<tbody>
<tr>
<td class = "checkbox_size_sm"><input type="checkbox"></td>
<td class = "image">ITEM</td>
<td class = "title"> </td>
<td>PRICE</td>
</tr>
<% #cart.line_items.order(created_at: :desc).each do |item| %>
<tr>
<td><%= check_box_tag "item_ids[]", item.id %></td>
<td class = "image"><%= image_tag item.product.item, size: "100" %>
</td>
<td class = "title" id = "td_align_middle">
<%= item.product.title %><br>
<%= item.product.brand %>
</td>
<td id = "td_align_middle"><%= item.product.price %></td>
</tr>
<% end %>
</tbody>
<tfoot>
<tr>
<td colspan = "4" class="total_price" >
Total Price <strong> <%= #cart.cart_total_price %> </strong>
</td>
</tr>
<tr>
<td colspan ="4" class="al-r">
<%= button_to "empty Cart", #cart, method: :delete %> <%= submit_tag "Delete item" %>
</td>
</tr>
</tfoot>
</table>
</div>
</div>
Help me..
wrap those buttons in a div, set the div to float left and overflow hidden
add hose two buttons in div with class divname and in css file add below style to
div.divname button { float:left; margin-right:10px; }
Related
I want that, depending on the width of the browser, tables should be created or deleted.
In CSS, you can use something like: `#media (min-width: 767px), whereas css-settings change, after width passes 767px. Is there something similar for ruby on rails? For example:
In my book/view.html.erb there is a table like this:
<table>
<tr>
<th>Title</th>
<th>Genre</th>
<th>Description</th>
</tr>
<tr>
<td><%= #book.title %></td>
<td><%= #book.genre %></td>
<td><%= #book.desc %></td>
</tr>
</table>
What I want is, that, after width gets smaller than 767px, it should look like this:
Title: <br>
<%= #book.title %><br>
Genre: <br>
<%= #book.genre %>
Description:
<%= #book.desc %>
How can I accomplish this? Thanks in advance!
Is there something similar for ruby on rails
Yes, it's called browser - a gem:
[Browser] adds a helper method called browser, that inspects your current user agent:
#app/views/controller/your_view.html.erb
<% if browser.tablet? || browser.mobile? %>
... do something ...
<% end %>
--
The problem with this, however, is that it only works when you render the page.
CSS Media queries adapt when the page changes dimension, meaning that if you decide to resize the app on your desktop, the CSS will immediately adapt.
Rails compiles code based on an HTTP request, meaning that it cannot "change" as CSS does. Whilst this shouldn't be a problem for tablets etc, it may cause issues for desktops.
Solution
What I want is, that, after width gets smaller than 767px, it should look like this
<% if browser.mobile? %>
Title: <br>
<%= #book.title %><br>
Genre: <br>
<%= #book.genre %>
Description:
<%= #book.desc %>
<% else %>
<table>
<tr>
<th>Title</th>
<th>Genre</th>
<th>Description</th>
</tr>
<tr>
<td><%= #book.title %></td>
<td><%= #book.genre %></td>
<td><%= #book.desc %></td>
</tr>
</table>
<% end %>
A completely CSS solution (highly recommended):
#app/assets/stylesheets/application.css
#media screen and (max-width: 767px) {
.big { display: none; }
.small { display: block; }
}
#app/view
<%= content_tag :div, class: "big" do %>
<table>
<tr>
<th>Title</th>
<th>Genre</th>
<th>Description</th>
</tr>
<tr>
<td><%= #book.title %></td>
<td><%= #book.genre %></td>
<td><%= #book.desc %></td>
</tr>
</table>
<% end %>
<%= content_tag :div, class: "small" do %>
Title: <br>
<%= #book.title %><br>
Genre: <br>
<%= #book.genre %>
Description:
<%= #book.desc %>
<% end %>
I use the nested forms to add a line of form for multiple data entry.
However all the style is lost on the generated forms using link_to.
Looking at the generated code, all the styles are there, but when I add the fields, it doesn't render the style.
I tried the partial that is being rendered as both table row and div row (bootstrap). The initially generated rows look perfect but the added row are all scrunched up and doesn't align with anything.
Thanks!
In application_helper
def link_to_add_fields(name, f, association, readonly)
new_object = f.object.send(association).klass.new
id=new_object.object_id
fields = f.fields_for(association, new_object, child_index: id) do |builder|
render("my partial form", f: builder, readonly: readonly)
end
link_to(name, "#", class: "add_fields", data: {id: id, fields: fields.gsub("\n", "")})
In my new.html
<table class="table table-bordered">
<thead>
<tr>
<th>Customer</th>
<th>Description</th>
<th>Notes</th>
<th></th>
</tr>
</thead>
<tbody>
<%= f.fields_for :my_association do |builder| %>
<%= render "my_partial", f: builder%>
<% end %>
<%= link_to_add_fields "Add New Line", f, :my_association, :readonly=>false%>
In my_partial
<tr>
<td><%= f.collection_select(:customer_id, #customers, :id, :fullname, :include_blank=>'Select') %>
<%= f.hidden_field(:id)%></td>
<td> <%= f.text_field(:description) %></div>
<td> <%= f.text_area(:note, :size=> "25x1") %></td>
<td> <%= f.hidden_field :_destroy %><%= link_to "remove", "#", class: "remove_fields"%> </td>
</tr>
This seems like a pure HTML/CSS issue.
Its <thead> instead of <theader> and wrap the partial in <tbody>
Try this:
<table class="table table-bordered">
<thead>
<tr>
<th>Customer</th>
<th>Description</th>
<th>Notes</th>
<th></th>
</tr>
</thead>
<tbody>
<%= f.fields_for :my_association do |builder| %>
<%= render "my_partial", f: builder%>
<% end %>
</tbody>
</table>
<div class="container-fluid">
<div class="row-fluid"><td colspan=6><%= link_to_add_fields "Add New Line", f, :my_association, :readonly=>false%></div></td>
</div>
I had to wrap my partial in it's own table and take out the row surrounding my call to the partial. This breaks the table but it's the only way thing will line up.
new.html.erb
<table class="table table-striped">
<thead>
<tr>
<th>Customer</th>
...
<th>Notes</th>
<th>*</th>
</tr>
</thead>
<tbody>
<%= f.fields_for :my_association do |builder| %>
<%= render "my_partial", f: builder%>
<% end %>
<tr>
<td colspan=6>
<%= link_to_add_fields "Add New Line", f, :my_association, :readonly=>false%>
</td></tr>
</tbody>
</table>
In my paritial
<table class="table table-striped">
<tbody>
<tr style="width:100%">
<td><%= f.collection_select(:customer_id, #customers, :id, :fullname, :include_blank=>'Select') %>
...
<td> <%= f.text_area(:note, :size=> "25x1") %></td>
<td> <%= f.hidden_field :_destroy %><%= link_to "remove", "#", class: "remove_fields"%> </td>
</tr>
</tbody>
</table>
I would love a better solution but this will work for now.
I have following tables in rails
<%= link_to 'New Voice', new_voice_path, class: "btn btn-danger btn-lg" %>
<table>
<tbody>
<% #voices.each do |voice| %>
<tr class = "opinion-list">
<td id = "opinion"><%= voice.opinion %></td>
<td><%= link_to 'Show', voice, class: "crud" %></td>
<td><%= link_to 'Edit', edit_voice_path(voice), class: "crud" %></td>
<td><%= link_to 'Destroy', voice, method: :delete, data: { confirm: 'Are you sure?' }, class: "crud" %></td>
</tr>
<% end %>
</tbody>
</table>
and it outputs this screen http://imgur.com/RBpOb2G
How can I add CSS class to each of the voice so that each individual "voice" can have margin? Right now it looks too crowded and reads like one huge paragraph so I'd like to have some spacing between them.
I tried adding margin/padding to opinion-list but that doesn't work.
I've also tried using the content_tag like so
<%= link_to 'New Voice', new_voice_path, class: "btn btn-danger btn-lg" %>
<table>
<tbody>
<% #voices.each do |voice| %>
<%= content_tag(:div, class: "box") do%>
<tr class = "opinion-list">
<td id = "opinion"><%= voice.opinion %></td>
<td><%= link_to 'Show', voice, class: "crud" %></td>
<td><%= link_to 'Edit', edit_voice_path(voice), class: "crud" %></td>
<td><%= link_to 'Destroy', voice, method: :delete, data: { confirm: 'Are you sure?' }, class: "crud" %></td>
</tr>
<%end%>
<% end %>
</tbody>
</table>
and style with the .box class but that also does not work.
I have a page that looks like the following.
Now I am trying to replicate this by using twitter boostrap <div class= hero-unit>. I have the done the following:
<div class="center hero-unit">
<table class="form">
<tr>
<td>
<%= form_for(#user) do |f| %>
<% if #user.errors.any? %>
<div id="error_explanation" xmlns="http://www.w3.org/1999/html">
<h2><%= pluralize(#user.errors.count, "error") %> prohibited this user from being saved:</h2>
<ul>
<% #user.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
</td>
</tr>
<tr>
<td class="th" colspan="2">Login Details</td>
</tr>
<tr>
<% if #user.id %>
<td><%= f.label :id %></td>
<td><%= #user.id %></td>
<% end %>
</tr>
<tr>
<td><%= f.label :current_sign_in_at, "Sign in time" %></td>
<td><%= #user.current_sign_in_at.nil? ? "Not signed in" : #user.current_sign_in_at.strftime('%d-%b-%Y %H:%M:%S') %></td>
</tr>
<tr>
<td> <%= f.label :first_name %>
<br/></td>
<td><%= #user.first_name %></td>
</tr>
<tr>
<td><%= f.label :last_name %>
<br/></td>
<td><%= #user.last_name %></td>
</tr>
<tr>
<td><%= f.label :email %>
<br/></td>
<td><%= #user.email %></td>
</tr>
<% if can? :manage, :all %>
<tr>
<td class="th" colspan=2>Roles and Privileges</td>
</tr>
<tr>
<% for role in Role.all %>
<td><%= role.name %><%= check_box_tag "user[role_ids][]", role.id, #user.roles.include?(role), :disabled => true %></td>
<% end %>
</tr>
<% end %>
<tr>
<td>
<%= link_to 'Edit', edit_user_path(#user), :class => 'btn btn-medium' %> |
<%= link_to 'Back', usermanagement_path, :class => 'btn btn-medium' %>
</td>
</tr>
</table>
<table class="form" >
<% if #user == current_user %>
<caption><b>Previous Orders</b></caption>
<thead>
<tr>
<th>Order No:</th>
<th>Order Date</th>
<th>Quantity</th>
<th>Total</th>
<th>View Order</th>
</tr>
</thead>
<% current_user.orders.each do |order| %>
<tr>
<td style="width: 10px;"><%= order.id %></td>
<td><%= order.created_at.to_s(:short) %></td>
<td><%= order.quantity %></td>
<td><%= number_to_currency(order.total_price, :unit => "£") %></td>
<td><%= link_to 'View Order',(order)%></td>
</tr>
<% end %>
<% if current_user.orders.empty? %>
<tr>
<td>No bookings found</td>
</tr>
<% end %>
</table>
</div>
<% end %>
<% end %>
The above changes output the following:
So what my question is how can I possibly get it to be aligned to the right of the login details. I did attempt to do the following <table style="float: left; margin-left: 20px;"> on the Previous Orders table. Why is this not working!
I have tried to mock the tables you have shown below. Since you are using Bootstrap I will advise you to take a look at Responsive Tables .There are various examples of how you can create and represent tables for smaller screens as well .
Here's the jsfiddle with the same Jsfiddle with tables
<div class="hero-unit">
<div class="row">
<div class="span4">
<table class="table table-condensed">
<thead>
<tr>
<th>Login Details</th>
</tr>
</thead>
<tbody>
<tr class="success">
<td>ID</td>
<td>1</td>
</tr>
<tr class="error">
<td>Sign in Time</td>
<td>01/04/2013 11:35am</td>
</tr>
<tr class="warning">
<td>First Name</td>
<td>Jack</td>
</tr>
<tr class="info">
<td>Last Name</td>
<td>Sparrow</td>
</tr>
<tr class="warning">
<td>Email Id</td>
<td>jack#sparrow.com</td>
</tr>
</tbody>
</table>
</div>
<div class="span8">
<table class="table table-condensed">
<thead>
<tr>
<th>Order no</th>
<th>Order Date</th>
<th>Quantity</th>
<th>Total</th>
<th>View order</th>
</tr>
</thead>
<tbody>
<tr class="success">
<td>1</td>
<td>TB - Monthly</td>
<td>01/04/2012</td>
<td>Approved</td>
<td>View Order</td>
</tr>
<tr class="error">
<td>2</td>
<td>TB - Monthly</td>
<td>02/04/2012</td>
<td>Declined</td>
</tr>
<tr class="warning">
<td>3</td>
<td>TB - Monthly</td>
<td>03/04/2012</td>
<td>Pending</td>
</tr>
<tr class="info">
<td>4</td>
<td>TB - Monthly</td>
<td>04/04/2012</td>
<td>Call in to confirm</td>
</tr>
</tbody>
</table>
</div>
</div>
How do I convert this gridview in razor syntax, I have curly brackets inside (data => { %>) ?
<%Html.GridView<Employee>(
Model,
data => { %>
<table class="grid" cellpadding="0" cellspacing="0">
<tr>
<th> </th>
<th> </td>
<th> </td>
<th>Name</th>
<th>E-mail</th>
</tr>
<% },
(item, css) => { %>
<tr class="<%=css%>">
<td><%=Html.ActionImage("Edit", "Home", new { Id = item.Id }, "~/Content/edit.gif", "Edit")%></td>
<td><%=Html.ActionImage("Delete", "Home", new { Id = item.Id }, "~/Content/delete.gif", "Delete")%></td>
<td> </td>
<td><%=item.Name%></td>
<td><%=item.Email%></td>
</tr>
<% },
"item",
"item-alternating",
item => { %>
<%using (Html.BeginForm("Save", "Home", new { Id = item.Id }, FormMethod.Post, new { id = "editForm" })) {%>
<tr class="item-edit">
<td><input type="image" runat="server" id="save" src="~/Content/ok.gif" alt="Update" /></td>
<td><%=Html.ActionImage("Index", "Home", null, "~/Content/cancel.gif", "Cancel")%></td>
<td> </td>
<td><%=Html.TextBox("Name", item.Name)%></td>
<td><%=Html.TextBox("Email", item.Email)%></td>
</tr>
<% } %>
<% },
data => { %>
<tr>
<td colspan="5"><hr /></td>
</tr>
<tr class="paging">
<td colspan="5">
<% if (data.PagedList.IsPreviousPage) { %>
<%=Html.ActionImage("Show", "Home", new { page = data.PagedList.PageIndex - 1 }, "~/Content/previous.gif", "Previous page")%>
<% } %>
<%=data.PagedList.TotalCount.ToString()%> records
<% if (data.PagedList.IsNextPage) { %>
<%=Html.ActionImage("Show", "Home", new { page = data.PagedList.PageIndex + 1 }, "~/Content/next.gif", "Next page")%>
<% } %>
</td>
</tr>
<tr>
<td colspan="5"> </td>
</tr>
<%using (Html.BeginForm("Add", "Home", FormMethod.Post, new { id = "addForm" })) {%>
<tr>
<th> </th>
<th> </td>
<th> </td>
<th>Name</th>
<th>E-mail</th>
</tr>
<tr>
<td><input type="image" runat="server" id="add" src="~/Content/add.gif" alt="Add" /></td>
<td> </td>
<td> </td>
<td><%=Html.TextBox("Name", "")%></td>
<td><%=Html.TextBox("Email", "")%></td>
</tr>
<% } %>
</tr>
</table>
<% });%>
Razor replaces the brackets <% ... %> with just the # symbol. So for example, this line
<%=Html.TextBox("Email", "")%>
becomes
#Html.TextBox("Email", "")
That's basically it. You'll just have to remove ALL of the <% ... %> brackets.