rows disappear when I try to add a form - ruby-on-rails

So, i'm trying to follow along with this example. I'm trying to translate it to my own own project where I have a set of rows that are displayed based on the results of applying the search criteria in a first form. I'm trying now to put a second form around the results to provide the administrator with checkboxes to be able to edit several of the rows displayed at the same time. however, when i try to put a form around the results, the results disappear altogether.
Here's the relevant piece of my controller:
def index
#search = Distribution.workflow.search(params[:traits_searchable_search])
respond_to do |f|
f.html {
#report = #search.paginate(:page => params[:page])
render :action => 'index'
}
f.csv do
send_data #report.to_csv, :type => "text/csv", :filename => "distribution_workflow_report.csv"
end
end
end
the view is nothing special. but i'm trying to wrap this tag (i've also tried removing the :method => :put piece and it's worth noting that the path provided to the form_tag is the page that's being displayed for now until i figure out how i'm going to get the routing to work):
<% form_tag admin_distributions_workflows_path, :method => :put do %>
around this table:
<table class="standard-grid">
<tr>
<th class="first"></th>
<th>ID</th>
<th>Customer</th>
<th>Customer Email</th>
<th>Resume URL</th>
<th>Partner</th>
<th>Partner Email</th>
<th>Status</th>
<th>Assigned To</th>
<th>Comments</th>
<th></th>
</tr>
<% #report.each do |row| %>
<tr>
<td><%= check_box_tag "row_ids[]", row.id %></td>
<td>
<%= row.owner.id %>
</td>
....
</td>
</tr>
<% end %>
</table>
<% end %>

Since Rails 3 you have to use <%= format with form_for and form_tag
<%= form_tag

Related

Rails - How to make Will-Paginate links show up BELOW the table?

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.

Ruby on Rails rendering all data from database

I am just starting out with Ruby on Rails and to practice, I am creating a simple blogging app. On the page I created to view the list of all posts, all the data that is passed to the page is being rendered in an array near the top of the page like so:
I can't figure out why the array is showing. Here's the controller for that page:
def index
#posts = Post.all
end
And heres the view:
<h1>Listing posts</h1>
<table>
<tr>
<th>Title</th>
<th>Text</th>
</tr>
<%= #posts.each do |post| %>
<tr>
<td><%= post.title %></td>
<td><%= post.text %> </td>
</tr>
<% end %>
</table>
and for the route I have tried both:
get '/posts/(.:format)' => 'posts#index'
and
get '/posts' => 'posts#index'
Also as a side question, what does (.:format) do anyway? The page seems to work the same whether I include that in the route or not.
When you use an "=" sign in the view it renders the result in html:
<%= #posts.each do |post| %>
Change the above line to:
<% #posts.each do |post| %>
The format is only useful when you want to render multiple formats (default is html), but you could call the page with a ".xls" or a ".json" etc and then have code in your controller to respond to those formats.

Ruby on Rails Render Partial

I have a model called Listing that I use to represent a listing for a sublet. I created a model called Filter that I use to filter the sublets based on a form that the user fills out. Upon filling out the form, I want the user to be redirected to a template that has all of the listings that were returned from the filter.
Here is my Filter model.
class Filter < ActiveRecord::Base
attr_accessible :air_conditioning, :available_rooms, :bathrooms, :furnished, :negotiable, :new, :parking, :maximum_price, :private_bathroom, :show, :term, :total_rooms, :utilities, :washer_dryer
serialize :term
def listings
#listings ||=find_listings
end
private
def find_listings
listings=Listing.order(:price)
listings=Listing.where("listings.price <= ?", maximum_price) if maximum_price.present?
listings=Listing.where(total_rooms: total_rooms) if total_rooms.present?
listings=Listing.where(available_rooms: available_rooms) if available_rooms.present?
listings=Listing.where(bathrooms: bathrooms) if bathrooms.present?
listings=Listing.where(term: term)
listings=Listing.where(furnished: furnished)
listings=Listing.where(negotiable: negotiable)
listings=Listing.where(utilities: utilities)
listings=Listing.where(air_conditioning: air_conditioning)
listings=Listing.where(parking: parking)
listings=Listing.where(washer_dryer: washer_dryer)
listings=Listing.where(private_bathroom: private_bathroom)
listings
end
end
Here is the show method for filter.
<p id="notice"><%= notice %></p>
<%= render (#filter.listings) %>
Pretty simple.
And here is the template called _listing.html.erb
<div style="padding:5px">
<%= link_to 'New Listing', new_listing_path,{:style=>'', :class => "btn"} %>
<h1>Available Sublets</h1>
<table id="listingTable" class="table table-bordered table-hover">
<tr>
<th><%= link_to 'Filter', new_filter_path,{:style=>'', :class => "btn"} %><%= link_to 'Clear Filter', listings_path, {:style=>'', :class => "btn"} %></th>
<th>Address</th>
<th><u><%= "Price Per Month" %></u></th>
<th>Description</th>
</tr>
<% if #listings !=nil %>
<% #listings.each do |listing| %>
<tr onmouseover="this.style.cursor='pointer';"
onclick="window.location.href = '<%= url_for(:controller => 'listings', :action => 'show', :id=>listing.id) %>' " >
<td><%= image_tag listing.photo.url(:small) %></td>
<td><%= listing.address %></td>
<td>$<%= listing.price %></td>
<td width="40%"><%= listing.description %></td>
</tr>
<% end %>
<% end %>
<% else if #listings==nil %>
<p> Sorry, No Sublets Fit Your Criteria! </p>
<% end %>
</table>
However, the filter never returns any results... I have tested atleast 20 times with queries that should definitely return atleast 1 listing. I feel like I have a naming convention problem but I have never used partials before. any help would be great.
This code:
listings=Listing.where(term: term)
listings=Listing.where(furnished: furnished)
listings=Listing.where(negotiable: negotiable)
listings=Listing.where(utilities: utilities)
listings=Listing.where(air_conditioning: air_conditioning)
listings=Listing.where(parking: parking)
listings=Listing.where(washer_dryer: washer_dryer)
listings=Listing.where(private_bathroom: private_bathroom)
Is not actually filtering listings down further. Basically, it's reassigning listings again and again and again.
If you want to apply successive filters to listings, do this:
listings = Listing.where(term: term)
listings = listings.where(furnished: furnished)
listings = listings.where(negotiable: negotiable)
...

Editing Quantity in Cart

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!

Rails forms helper: dealing with complex forms and params (it's my first day with rails)

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.

Resources