Rails - will_paginate has broken link_to - ruby-on-rails

I've added the will_paginate gem, and it's working fine on all other areas of site. However, whenever I try clicking in the 'Show' links in the view, to link to the order, it's giving me this error message: *
The #orders variable appears to be empty. Did you forget to pass the collection object for will_paginate?*
Anyone have any ideas why this is happening and how I can fix it? I've tried restarting the server. Thanks!!
View code:
<h3>Your Orders:</h3>
<table class="order-items">
<th>Date</th>
<th>Order No.</th>
<th>View Order</th>
<% #orders.each do |order| %>
<tr>
<td><%= order.created_at.strftime("%d %b. %Y") %></td>
<td><%= order.id %></td>
<td><%= link_to 'Show', order_path(order) %></td>
</tr>
<% end %>
</table>
<p><%= will_paginate #orders %></p>
Controller Code:
#customer = Customer.find(params[:id])
#orders = #customer.orders.paginate page: params[:page], order: 'id desc',
per_page: 5
Routes:
resources :orders
resources :customers
get 'admin' => 'admin#index'
get 'account' => 'customers#show'
match '/account', :to => 'customers#show'
match'/signup', :to => 'customers#new'
controller :sessions do
get 'login' => :new
post 'login' => :create
delete 'logout' => :destroy
end
resources :users
resources :line_items
resources :carts
get "store/index"
resources :products do
get :who_bought, on: :member
end
resources :line_items do
put 'decrease', on: :member
put 'increase', on: :member
end
root to: 'store#index', as: 'store'

Related

form not updating for specific record rails

I'm trying to update a record with a button, by having and admin user tyoe confirm a record by clicking the confirm submit button with this form, but it would just update the first record in the database, not the specific one. Can i put "hour.id" in the form_for section to make it update the record i want it to?
index.html.erb form view
<% #allhours.each do |hour| %>
<tr id="dashfield">
<td><%= hour.user.first_name + " " +hour.user.last_name %></td>
<td><%= hour.assignment %></td>
<td><%= hour.hours %></td>
<td><%= hour.supervisor %></td>
<td><%= hour.date %></td>
<td><%= link_to "confirm", edit_hour_path(hour.id, :status => 'confirmed')%></td>
</tr>
<% end %>
Controller
def index
#allhours = HourLog.where(status:'pending')
end
def update
#hour = HourLog.find_by(status:'pending')
#hour.update_attribute(update_hour_params)
redirect_to '/dashboard/hours'
end
def edit
#hour = HourLog.find(params[:id])
#hour.update_attributes(update_hour_params)
redirect_to '/dashboard/hours'
end
hour_log model
class HourLog < ActiveRecord::Base
belongs_to :user
end
routes
Rails.application.routes.draw do
root 'welcome#index'
get '/signup' => 'users#new'
resources :users
get '/dashboard/users' => 'users#index'
put '/dashboard/users' => 'users#update'
get '/login' => 'sessions#new'
post '/login' => 'sessions#create'
delete '/logout' => 'sessions#destroy'
get '/dashboard' => 'hours#new'
get '/dashboard/hours' => 'hours#index'
put '/dashboard/hours' => 'hours#update'
resources :hours
Thanks!
This line :
#hour = HourLog.find_by(status:'pending')
will find the first Hourlog that is in pending status. You should pass the hourlog id in the form and use it in the controller
change the line in the routes :
put '/dashboard/hours/:id' => 'hours#update'
in the view :
<%= form_tag dashboard_hours_path(id:hour.id), method: :put do %>
<%= hidden_field_tag :role, :value => 'confirm'%>
<%= submit_tag 'Confirm'%>
<% end %>
in the controller :
#hour = HourLog.find(params[:id])

Rails link_to the records of a instance variable .count() result

I have records in my Submitter model being shown in Application Index view being displayed in a table like below. Only I want to be able to click on the number that's listed for the .count() and be taken to my Tickets Index view with all of the corresponding records (Tickets) listed.
I'm having trouble understanding how I can link instance variables to a controller's action, in order to use the layout of my views.
Any ideas?
<table>
<tr>
<th>Name</th>
<th>Number of Tickets</th>
</th>
<tr>
<% Submitter.where(:school_id => current_user.school_id, :disabled => false).each do |sub| %>
<td><%= link_to sub.full_name, edit_submitter_path(sub) %></td>
<td><%= Ticket.where(:submitter_id => sub).count %></td>
</tr>
<% end %>
</table>
routes.rb
resources :feedbacks
resources :products
get "password_resets/new"
get 'signup', to: 'users#new', as: 'signup'
get 'login', to: 'sessions#new', as: 'login'
get 'logout', to: 'sessions#destroy', as: 'logout'
resources :users
resources :sessions
resources :password_resets
resources :mailing_lists
resources :submitters
resources :emails
resources :notes
resources :reminders
resources :descriptions
resources :issues
resources :locations
resources :schools
resources :tickets
root :to => "application#index"
It sounds like you have a submitter that has many tickets. If so, you'd show a list of submitters (in the submitters_controller#index method) and count the number of tickets. Then link that number to the tickets_controller#index method. Like this...
routes.rb
resources :submitters do
resources :tickets
end
submitters_controller.rb
class SubmittersController < ApplicationController
def index
#submitters = Submitter.where(:school_id => current_user.school_id, :disabled => false)
end
end
tickets_controller.rb
class TicketsController < ApplicationController
def index
#submitter = Submitter.find(params[:submitter_id])
#tickets = #submitter.tickets
end
end
views/submitters/index.html.erb
<table>
<tr>
<th>Name</th>
<th>Number of Tickets</th>
</th>
<% #submitters.each do |sub| %>
<tr>
<td><%= link_to sub.full_name, edit_submitter_path(sub) %></td>
<td><%= link_to pluralize(sub.tickets.count, 'ticket'), submitter_tickets_path(sub) %></td>
</tr>
<% end %>
</table>
views/tickets/index.html.erb
# This would show a list of tickets and their attributes - possibly linking to a ticket.
Something like this maybe? Not 100% understanding your question.
<table>
<tr>
<th>Name</th>
<th>Number of Tickets</th>
</th>
<tr>
<% Submitter.where(:school_id => current_user.school_id, :disabled => false).each do |sub| %>
<td><%= link_to sub.full_name, edit_submitter_path(sub) %></td>
<td><%= link_to pluralize(sub.tickets.count, 'ticket'), tickets_path %></td>
<% end %>
</tr>
</table>
I, of course, can't give you perfectly working code unless I know what your config/routes.rb looks like, since I don't know what is the method for your Tickets Index view.

Generating wrong URL in the form action using form_for in rails4

I am using rails 4.0.0. I am a newcomer to rails and learning rails watching video tutorials and stuck in a problem so need some help.
I have a controller called Subjects and following is the code
Subject Constoller
class SubjectsController < ApplicationController
before_action :set_subject, only: [:show, :edit, :update]
def index
#subjects = Subject.all
end
def list
#subjects = Subject.all
##subjects = Subject.order("subjects.position ASC")
end
def show
#subject = Subject.find(params[:id])
end
def new
#subject = Subject.new(:name => "default")
end
def create
#subject = Subject.new(subject_params)
if #subject.save
redirect_to(:action => 'list')
else
render('new')
end
end
def edit
end
def update
if #subject.update(subject_params)
redirect_to #subject
else
render("edit")
end
end
def delete
#subject = Subject.find(params[:id])
end
def destroy
Subject.find(params[:id]).destroy
redirect_to(:action => "list")
end
private
# Use callbacks to share common setup or constraints between actions.
def set_subject
#subject = Subject.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def subject_params
params.require(:subject).permit(:name, :position, :visible)
end
end
I created a view for the List Action
View for the List Action
<h1>List</h1>
<div class="subject list">
<h2>Subjects</h2>
<table class="listing" summary="Subject list">
<tr class="header">
<th>Position</th>
<th>Subject</th>
<th>Visible</th>
<th>Pages</th>
<th>Actions</th>
</tr>
<% #subjects.each do |subject| %>
<tr>
<td><%= subject.position %></td>
<td><%= subject.name %></td>
<td class="center"><%= subject.visible ? 'Yes' : 'No' %></td>
<td class="center"><%= subject.pages.size %></td>
<td class="actions">
<%= link_to 'Show', subject, :class => 'action show'%>
<%= link_to "Edit", edit_subject_path(subject), :class => 'action edit' %>
<%= link_to "Delete", {:action => 'delete', :id => subject.id}, :class => 'action delete' %>
</td>
</tr>
<% end %>
</table>
</div>
I also have a view for delete and code is as follows
View for Delete Action
<h1>Delete</h1>
<%= link_to("<< Back to List", {:action => 'list'}, :class => 'back-link') %>
<div class="subject delete">
<h2>Delete Subject</h2>
<%= form_for(:subject, :url => {:action => "destroy", :id => #subject.id}) do |f|%>
<p>Are you sure you want to permanently delete this subject?</p>
<p class="reference-name"><%= #subject.name %></p>
<div class="form-buttons">
<%= submit_tag("Delete Subject") %>
</div>
<% end %>
</div>
Here is the configuration of my routes file
SimpleCms::Application.routes.draw do
# The priority is based upon order of creation: first created -> highest priority.
# See how all your routes lay out with "rake routes".
# You can have the root of your site routed with "root"
# root 'welcome#index'
root 'demo#index'
# Example of regular route:
# get 'products/:id' => 'catalog#view'
get 'demo/index'
get 'demo/hello'
get 'demo/other_hello'
#get 'subjects/index'
get 'subjects/list'
get 'subjects/delete'
get '/subjects/:id/destroy' => 'subjects#destroy'
resources :subjects
get 'subjects/show'
get 'subjects/new'
get 'subjects/:id' => 'subjects#show'
# Example of named route that can be invoked with purchase_url(id: product.id)
# get 'products/:id/purchase' => 'catalog#purchase', as: :purchase
# Example resource route (maps HTTP verbs to controller actions automatically):
# resources :products
#resources :subjects
# Example resource route with options:
# resources :products do
# member do
# get 'short'
# post 'toggle'
# end
#
# collection do
# get 'sold'
# end
# end
# Example resource route with sub-resources:
# resources :products do
# resources :comments, :sales
# resource :seller
# end
# Example resource route with more complex sub-resources:
# resources :products do
# resources :comments
# resources :sales do
# get 'recent', on: :collection
# end
# end
# Example resource route with concerns:
# concern :toggleable do
# post 'toggle'
# end
# resources :posts, concerns: :toggleable
# resources :photos, concerns: :toggleable
# Example resource route within a namespace:
# namespace :admin do
# # Directs /admin/products/* to Admin::ProductsController
# # (app/controllers/admin/products_controller.rb)
# resources :products
# end
end
So the main concept is as follows when i will call http://localhost:3000/subjects/list it will show all the subjects and there is also three button that is show, edit and delete and after clicking on the delete link it will go to the delete view and after clicking on the submit button of the form it will be deleting the particular item.
But the main problem is that the form_for in delete view is producing the url in the form action is /subjects/8 where 8 is the id of the subject to be deleted but the url should be /subjects/destroy?id=8 to delete the item but when id is passed along with the action in form_for it produces the wrong url but when only the action is passed it produces the url as /subjects/destroy.
I cannot figure out what is wrong so please help. Thanks to all in advance.
Try a link with delete method instead use a form.
<p>Are you sure you want to permanently delete this subject?</p>
<p class="reference-name"><%= #subject.name %></p>
<%= link_to 'Delete Subject', #subject, method: :delete %>
UPDATE
If you want to use a form for destroy action, try this :
<% form_for #subject, :html => {:method => :delete} do |form| %>
Try this:
<%= form_for(#subject, :url => {:action => "destroy") do |f|%>
Instead of this:
<%= form_for(:subject, :url => {:action => "destroy", :id => #subject.id}) do |f|%>
You don't need to mention the id because in form_for it takes id of the variable mentioned by default.

Rails - will_paginate gem not working when using .each do

EDIT: I've altered the code to now read:
View code:
<h3>Your Orders:</h3>
<table class="order-items">
<th>Date</th>
<th>Order No.</th>
<th>View Order</th>
<% #orders.each do |order| %>
<tr>
<td><%= order.created_at.strftime("%d %b. %Y") %></td>
<td><%= order.id %></td>
<td><%= link_to 'Show', order_path(order) %></td>
</tr>
<% end %>
</table>
<p><%= will_paginate #orders %></p>
Controller Code:
#customer = Customer.find(params[:id])
#orders = #customer.orders.paginate page: params[:page], order: 'id desc',
per_page: 5
Routes:
resources :orders
resources :customers
get 'admin' => 'admin#index'
get 'account' => 'customers#show'
match '/account', :to => 'customers#show'
match'/signup', :to => 'customers#new'
controller :sessions do
get 'login' => :new
post 'login' => :create
delete 'logout' => :destroy
end
resources :users
resources :line_items
resources :carts
get "store/index"
resources :products do
get :who_bought, on: :member
end
resources :line_items do
put 'decrease', on: :member
put 'increase', on: :member
end
root to: 'store#index', as: 'store'
The problem is now that I'm getting the error:
The #orders variable appears to be empty. Did you forget to pass the collection object for will_paginate?
whenever I click on a "Show" link in the list of orders in the view. Tried restarting the server just in case, but can't get it working :S!
It should be
<% #orders.each do |order| %>
not
<% #customer.orders.each do |order| %>
Pls check this code
#customer = Customer.find(params[:id])
#orders = #customer.orders.paginate page: params[:page], order: 'title',per_page: 5
in view
<% #orders.each do |order| %>

Rails: url_to scope

In the following piece of code (of course in a view) I have <td><%= post.full_hsh %></td> displaying correctly the value of the field post.full_hsh, but url_for gives an error: No route matches {:controller=>"posts", :action=>"edit", :id=>nil}.
Why is post.full_hsh correct in a row and nil in the row below? Thanks
<% #posts.each do |post| %>
<tr>
<td><%= truncate post.body, :length => 100, :separator => ' ' %>...</td>
<td><%= post.created_at %></td>
<td><%= post.full_hsh %></td>
<td><%= url_for :controller => 'posts', :action => 'edit', :id => post.full_hsh %></td>
</tr>
<% end %>
EDIT:
my routes.rb file:
get "admin/index"
root :to => 'home#index'
#access posts via /posts/ab123c or /p/ab123c
resources :posts, :path => 'p', :except => [:new, :index, :delete]
resources :posts, :except => [:new, :index, :delete]
match '/p/:id/delete' => 'posts#delete', :as => 'posts_delete'
match '/admin' => 'admin#index'
post.full_hsh is an attribute of the post objection. you probably want :id => post instead
You want to generate the URL based on the object itself, in this case post, not on the attribute of an object (post.full_hsh)

Resources