I just came across the will-paginate gem. I'm trying to separate records into multiple pages. I managed to make some progress, but I ran into a problem.
The Pagination Tab (that shows the page numbers) appears above the table. It is also uncentered. How can I have the tab appear below the table.
This is how it currently looks
<= Previous 1 2 Next =>
Table Data
How I would like it to look
Table Data
<= Previous 1 2 Next =>
Here is my code
<tbody>
<% g = Game.where(console: #title.split(' ').first(2).join(' ')).order(title: :asc).paginate(:page => params[:page], :per_page => 2) %>
<% g.each do |game| %>
<tr>
<td> <%= link_to "#{game.title}", game %></td>
<td> <%= game.genre %></td>
<td> <%= game.release_date %></td>
<td> <%= game.rating %></td>
<% if signed_in? && current_user.admin? %>
<div>
<td id = 'actions'><%= link_to 'Show', game, class: 'action' %></td>
<td id = 'actions'><%= link_to 'Edit', edit_game_path(game), class: 'action' %></td>
<td id = 'actions'><%= link_to 'Delete', game, method: :delete, class: 'action' %></td>
</div>
<% end %>
</tr>
<%= will_paginate g %>
<% end %>
</tbody>
I even put will_paginate at the end of my code block.
Your markup is invalid. Your call to will_paginate is inside of the table, in between tags.
If you want it to show up below, above, or anywhere else, just put the call to pagination there.
<%= will_paginate collection %>
You really should be instantiating instance variables in your controller. This line should not be in your view.
<% g = Game.where(console: # ...
This way you solve the restriction of where to place your pagination call, and you write more idiomatic Rails code.
If you must go down this road, and I strongly discourage you, just do load your collection above the table.
<% g = Game.where(console: #title.split(' ').first(2).join(' ')).order(title: :asc).paginate(:page => params[:page], :per_page => 2) %>
Then, below that, render the pagination since the variable g now exists:
<%= will_paginate g %>
Then render your table.
Related
I have got a problem with saving multiple record.
This script wil load a list of instruments that belong to a department through a join table.
This form will make a new record for another join table, the problem is when I'f got 4 instruments it only will save the last instrument.
Image generated list
Can anybody help me out to solve this problem or point me into the right direction ??
<%= form_for(:joindaylisting) do |j| %>
<% #instrumentslist.each do |instrument| %>
<tr class="<%= cycle('odd', 'even') %>">
<td>
<% j.label(:instrument_id, "#{instrument.name}") %>
<%= link_to("#{instrument.name}", {:controller => 'instruments', :action => 'show_instruction', :instrument_id => instrument.id}, :onclick=>"window.open('height=670, width=675');return false;") %>
</td>
<%= j.hidden_field(:instrument_id, :value => instrument.id) %>
<td></td>
<% j.label(:ammountdesinfection, "") %>
<td><%= j.text_field(:ammountdesinfection) %></td>
<% j.label(:ammountinstruments, "") %>
<td><%= j.text_field(:ammountinstruments) %></td>
<% j.label(:ammountrelease, "") %>
<td><%= j.text_field(:ammountrelease) %></td>
<% j.label(:notes, "") %>
<td><%= j.text_area(:notes) %></td>
</tr>
<% j.label(:department_id) %>
<%= j.hidden_field(:department_id, :value => #department.id) %>
<% end %>
<% end %>
Only the last one is saved because the fields have the same name and the last one overrides all other values. See the params you are getting on the receiving controller.
You probably want/need to configure departments to accepts_nested_attributes_for :instruments and a form for the department object with fields_for: instruments. I've highlighted keywords that my help you get the information you want, the Rails Guides is a good place to start.
So this is another post about updating the quantity in the cart! Any one that I could find seemed to be outdated, so I apologize if this seems repetative.
But I am following along in Agile Web Development with Rails 4th edition book, and they were so kind as to leave editing the quantity as a 'challenge' and not show the answer :D. Now as I'm trying to get it to work I'm having troubles.
Show in my views/cart/show.html.erb I have the following table
<table>
<tr>
<th>Quantity</th>
<th>Product Name</th>
<th>Size</th>
<th>Price</th>
</tr>
<% #cart.line_items.each do |item| %>
<tr>
<td>
<%= form_for 'item', :url => {:controller => 'line_items', :action => 'update', id: item} do |f| %>
<div class="field">
<%= f.number_field :qty, :value => item.qty %>
<%= submit_tag "Update" %>
</div>
<% end %>
</td>
<td><%= item.product.name %></td>
<td><%= item.size %></td>
<td><%= number_to_currency(item.total_price) %></td>
</tr>
<% end %>
<tr>
<td colspan="3">Total Price</td>
<td><%= number_to_currency(#cart.total_price) %></td>
</tr>
</table>
Yet when I click update I get either
Unknown action
The action '29' could not be found for LineItemsController
or
Unknown action
The action '35' could not be found for LineItemsController
even if I completely take out the id field. I can deal with the update function on the controller side and getting it update properly - I want to figure that out on my own but I can't figure out what could possibly be generating these numeric actions and how I can fix it. In short, what is producing this error and how can I fix it? Is it perhaps related to the fact I have a line_item form in a cart view?
do you check the 29 and 35 is either ur id or anything else? try to check with your database for LineItems , and how your controller look like?? and
<%= form_for 'item', :url => {:controller => 'line_items', :action => 'update', id: item} do |f| %>
you trying to update it in ajax way or ? when update the quantity, should it be using ajax if it's not mistaken (the book asked to do in that way right? )
So I got it working - that I did was I tweaked the form header like so
<%= form_for :item, :url => line_items_update_path(id: item.id) do |f| %>
I added the following line to my routes.rb
get "line_items/update"
And added one line to my line_items_controller
def update
#line_item = LineItem.find(params[:id])
#line_item.qty = params[:item][:qty] #added this line here
For those who are having problems!
I have created a scaffold Phone and index.html.erb shows a simple table with list of phone and edit/delete options. Now i want to add jquery checkboxes so I can do bulk delete or move actions. See attached image here
Can someone give me any idea/pointers on how to do it ?
Edit - this is the index.html.erb file
<% #phones.each do |phone| %>
<tr>
<td><%= phone.model %></td -->
<td><%= phone.type %></td>
</tr>
<% end %>
there is no form here so I am not sure I can use form_tag helpers or am I confusing something ?
Thanks
This would be a starting point, a form with the check boxes.
<%= form_tag(:controller => "phone", :action => "bulk_update", :method => "PUT") %>
<%= check_box_tag(:blackberry) %>
<%= label_tag(:pet_dog, "Blackberry") %>
<%= check_box_tag(:Nokia) %>
<%= label_tag(:pet_cat, "Nokia") %>
<%= submit_tag("Update") %>
<% end %>
Then a controller action called bulk_update and you could delete/update the records based on what has been submitted.
EDIT: You will also want to create a route in routes.rb for this.
Wrap the table in a form helper.
<%= form_tag foo_path do %>
# ...
<% #phones.each do |phone| %>
<tr>
<td><%= check_box_tag "selected[]", phone.id %></td>
<td><%= phone.model %></td>
<td><%= phone.type %></td>
</tr>
<% end %>
# ...
<%= button_tag "Do something" %>
<% end %>
This creates a form, with a checkbox in every row of the table. Replace foo_path with the route helper that you want to use. The value of the selected checkboxes will be passed to your controller action in the array params[:selected], where you can do with them as you wish. The values of each checkbox will be the id for the corresponding phone object.
I am working on my first web and Rails app and can not figure out how to get a search feature work from my main page to one of the controllers.
How to send the request and redirect to a results page to show the results from the search.
I can't get this to work as am not sure how to route in a way my variable #histories will keep results and display on the show page.
I would appreciate some insight into search from any page and displaying results on a dedicated page.
Here is what i have so far in terms of the controller, model and partials.
Shipments Model:
def self.search(search)
search_condition = search
find_by_sql("SELECT cargo_transit_histories.current_location,cargo_transit_histories.updated_at FROM cargo_transit_histories
INNER JOIN shipments ON shipments.id = cargo_transit_histories.shipment_id WHERE shipments.tracking_number='search_condition'")
end
Tracking Controller:
def search
#histories = Shipment.search(params[:search])
render('show')
end
Show (Found in Tracking view):
<div class="search_result">
<%= render 'track/search_results' %>
</div>
_search (partial):
<%= form_tag :controller => 'tracking', :action => 'search', :method => 'get' do %>
<%= text_field_tag :search, params[:search], :id => 'search_field' %>
<%= submit_tag "Search", :name => nil %>
<%= link_to_function "Clear", "$('search_field').clear()" %>
<% end %>
_search_results (partial):
<div class="Results list">
<table class="Resultslisting" summary="Result list">
<tr class="header">
<th>Current Location</th>
<th>Date/Time</th>
</tr>
<% if !#histories.empty? %>
<% #histories.each do |result| %>
<tr>
<td><%= result.current_location %></td>
<td><%= result.updated_at %></td>
</tr>
<% end %>
</table>
<% else %>
<p> The tracking number does not exist!</p>
<% end %>
</div>
Try something like the following adapted example:
https://gist.github.com/4173716
But you need to dig a bit deeper into Rails 3 to understand why.
Learning rails and something smells a little funny.
I have the following form for updating quantities in a shopping cart.
<% form_for(:cart, #cart.items, :url => { :action => "update_cart" }) do |f| %>
<table>
<tr>
<th>Item</th>
<th>Quantity</th>
<th>Price</th>
</tr>
<% for item in #cart.items %>
<% f.fields_for item, :index => item.id do |item_form| %>
<tr>
<td><%=h item.title %></td>
<td><%=item_form.text_field :quantity, :size => 2 %>
<span># <%=h number_to_currency(item.item_price) %></span></td>
<td><%=h number_to_currency(item.line_price) %></td>
</tr>
<% end %>
<% end %>
<tr>
<td colspan="2">Total:</td>
<td><%=h number_to_currency(#cart.total_price) %></td>
</tr>
</table>
<%=submit_tag 'Update Cart' %>
<% end %>
In my update_cart action, I iterate through the existing cart items and set the new quantity:
def update_cart
#cart = find_cart
#cart.items.each do |item|
quantity = params[:cart][:cart_item][item.id.to_s][:quantity].to_i
#cart.update_quantity_for(item, quantity)
end
redirect_to :action => 'cart'
end
I dont have a REST interface controller for carts or cart items. Is there a better way to deal with this deep params data structure? The expression params[:cart][:cart_item][item.id.to_s][:quantity].to_i strikes me as dangerous for invalid form data.
The correct way to do this is the use the "accepts_nested_attributes" attribute in the cart model. Then you can just use CartController's update method to save your items. (http://railscasts.com/episodes/196-nested-model-form-part-1)
Also, your total price should probably be a method defined in the Cart model.