Rails 3 task scopes? - ruby-on-rails

I am working on a simple rails 3 todo application and I am trying to filter the app by completed tasks and none completed task but whenever I try calling a scope I get the error message.
undefined method `completed' for #<Array:0x007fe8420d0e58>
task.rb
scope :completed , where(:completed => true)
scope :incomplete , where(:finished => false)
index.html.erb
<table>
<tr>
<th>Name</th>
<th>Description</th>
<th>Finished</th>
<th>User</th>
</tr>
<% #tasks.each do |task| %>
<tr>
<td><%= task.name %><%= button_to "complete", complete_task_path(task.id)%></td>
<td><%= task.description %></td>
<td><%= task.finished %></td>
<td><%= task.user_id %></td>
<td><%= link_to 'Show', task %></td>
<td><%= link_to 'Edit', edit_task_path(task) %></td>
<td><%= link_to 'Destroy', task, confirm: 'Are you sure?', method: :delete %></td>
</tr>
<% end %>
</table>
<%= content_tag :h2, "Stuff Ive done" %>
<table>
<tr>
<th>Name</th>
</tr>
<% #tasks.completed.each do |task| %>
<tr>
<td><%= task.name %></td>
</tr>
<% end %>
</table>
task_controller.rb
def complete
#task = Task.find(params[:task_id])
#task.completed = true
#task.save
redirect_to task_path
end
routes.rb
match "tasks/:id/complete" => "task#complete", :as => :complete_task
Any reasons why rails is giving me this error?

Just by looking at your view (index.html.erb), in one place, you are treating as a relation.
<% #tasks.each do |task| %>
Later in the code, you are trying to access it as a single object.
<% #tasks.completed.each do |task| %>
Since you are seeing the error on the second instance, you need to access 'completed' as in:
<% #tasks.completed.each do |task| %>
<% completed = task.completed %>
<% completed.each do |com| %>
Does this make sense?

Related

submit_tag with check_box_tag returning error

I'm having some trouble with check_box_tag and my method enable (a product can be disabled) in Ruby on Rails.
I created some checkbox so the user select the products that he wants to set enable = true.
Keeps returning the error "Couldn't find Product with 'id'=enable"
My method enable:
def enable
Product.update_all(["enable=?", true], :id => params[:selected_products])
redirect_to root_url
end
My routes.rb:
Rails.application.routes.draw do
get 'product_export/new'
get 'product_imports/new'
resources :users
resources :product_imports
resources :products do
collection do
post :import
post :export
post :enable
end
end
root to: 'products#index'
end
and finally my view with the table e the check_box_tag:
<%= form_tag enable_products_path, :method => :put do %>
<%= submit_tag "Enable products" %>
<table class="table table-striped table-responsive" id="productsTable">
<thead class="thead-inverse">
<tr>
<th/>
<th>Code</th>
<th>Enable</th>
<th>Bar code</th>
<th>Unit cost</th>
<th>Description</th>
<th>Family</th>
<th>Final</th>
<th>Measure</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% #products.each do |product| %>
<tr>
<td><%= check_box_tag 'selected_products[]', product.id %></td>
<td><%= link_to (product.code), edit_product_path(product) %></td>
<td><%= product.enable %></td>
<td><%= product.bar_code %></td>
<td><%= product.unit_cost %></td>
<td><%= product.description %></td>
<td><%= product.family %></td>
<td><%= product.final %></td>
<td><%= product.measure %></td>
<td><%= link_to 'Destroy', product, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<% end %>
Thanks for the help!
Use where to find selected products and when to update them:
Product.where(id: params[:selected_products]).update_all(enable: true)

What is error with cancan in view

I am using cancan to authorize the user in controller,And in views i am using Can?to check if the user is authorize to see that function
index.html.erb
<p id="notice"><%= notice %></p>
<h1>Listing Noticeboards</h1>
<table class="table table-bordered table-striped">
<thead>
<tr>
<th>society</th>
<th>Notice heading</th>
<th>Notice text</th>
<th>Active</th>
<th>Isdelete</th>
<th>Expire</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% #noticeboards = #noticeboards.where(Isdelete: 0).find_each %>
<% #noticeboards.each do |noticeboard| %>
<tr>
<td><%= Society.find_by(id: noticeboard.id_society, Isdelete: 0).name %></td>
<td><%= noticeboard.notice_heading %></td>
<td><%= noticeboard.notice_text %></td>
<td><%= noticeboard.active %></td>
<td><%= noticeboard.IsDelete %></td>
<td><%= noticeboard.Expire %></td>
<td><%= link_to 'Show', noticeboard %></td>
<% if can? :update, noticeboard %>
<td><%= link_to 'Edit', edit_noticeboard_path(noticeboard) %> <% end %></td>
<% if can? :delete , noticeboard %>
<td>
<%= link_to 'Destroy', noticeboard, method: :delete, data: { confirm: 'Are you sure?' } %></td> <% end %>
</tr>
<% end %>
</tbody>
</table>
<br>
<% if can? :create, #noticeboard %>
<%= link_to 'New Noticeboard', new_noticeboard_path %>
<% end %>
In this if i change :delete to anything else, say :Hello, its still checking can, and if i am not using anything, it gives error saying "wrong number of arguments" what is the error, why its not taking :delete function?

Simple range search in Rails

I'm new to ROR and I'm trying to implement a simple search which should allow a user to search for hikes that match a certain range of difficulty. The form is properly displayed and I do not get any error messages, the problem is that no search results are dispayed.
I've been trying to solve this for a whole day now, so any help is extremely appreciated!
Index.html.erb
<%= form_tag wanderungens_path, :method => 'get' do %>
<p>
<%= number_field_tag ':search[dauer_von]', #search.dauer_von %>
<%= number_field_tag ':search[dauer_bis]', #search.dauer_bis %>
<%= submit_tag "Search", :name => nil %>
</p>
<% end %>
Wanderungens_controller.rb
def index
#search = WanderungenSearch.new(params[:search])
#wanderungens = #search.scope
end
wanderungen_search.rb
class WanderungenSearch
attr_reader :dauer_von, :dauer_bis
def initialize(params)
params ||= {}
#dauer_von = params[:dauer_von]
#dauer_bis = params[:dauer_bis]
end
def scope
Wanderungen.where('zeitdauer BETWEEN ? AND ?', #dauer_von, #dauer_bis)
end
I followed this tutorial: https://www.youtube.com/watch?v=Ri1YNjl1_hA
I'm on Rails 4.0.0 and Ruby 2.0.0
Update: This is the code where #Wanderungens is displayed:
<table>
<thead>
<tr>
<th>Description</th>
<th>User</th>
<th>Schwierigkeitsgrad</th>
<th>Zeitdauer</th>
<th></th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<% #wanderungens.each do |wanderungen| %>
<tr>
<td><%= wanderungen.description %></td>
<td><%= wanderungen.user.email if wanderungen.user %></td>
<td><%= wanderungen.schwierigkeitsgrad.description if wanderungen.schwierigkeitsgrad %></td>
<td><%= wanderungen.zeitdauer if wanderungen.zeitdauer %></td>
<td><%= link_to 'Show', wanderungen %></td>
<td><%= link_to 'Edit', edit_wanderungen_path(wanderungen) %></td>
<td><%= link_to 'Destroy', wanderungen, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
If I search from 1 to 2, the following request is generated:
http://localhost:3000/wanderungens?utf8=%E2%9C%93&%3Asearch[dauer_von]=1&%3Asearch[dauer_bis]=2

ruby printing database content

I am new in Ruby.
I have to render the database structure, using tutorial i found suitable code:
File index.html.erb:
<h1>Listing tasks</h1>
<table>
<tr>
<th>Title</th>
<th>Content</th>
<th>Date From</th>
<th>Date To</th>
<th></th>
<th></th>
<th></th>
</tr>
<% #tasks.each do |task| %>
<tr>
<td><%= task.title %></td>
<td><%= task.content %></td>
<td><%= task.datefrom %></td>
<td><%= task.dateto %></td>
<td><%= link_to 'Show', task %></td>
<td><%= link_to 'Edit', edit_task_path(task) %></td>
<td><%= link_to 'Destroy', task, :method => :delete, :data => { :confirm => 'Are you sure?' } %></td>
</tr>
<% end %>
</table>
<br />
<%= link_to 'New Task', new_task_path %>
Now i want to group tasks by 'date from' value, probably it means that in index.html.erb i should print only dates, and clicking on the date i should call another html file that will render tasks by chosen date.
this can be like
File index.html.erb:
<h1>Listing dates</h1>
<table>
<tr>
<th>Date From</th>
<% #tasks.each do |task| %>
<tr>
<td><%= !!! IF DATE NOT PRINTED THEN PTINT IT task.datefrom %></td>
<td><%= link_to 'Edit', edit_date_path(task, date) %></td>
</tr>
<% end %>
</table>
<br />
<%= link_to 'New Task', new_task_path %>
Where edit_date_path(task, date) should refer me to the index_date.html.erb, where i can get selected date and print tasks according to selected date.
Maybe i can get suggestion, it will be much easier is fomebody can help me with that, as task should not be very difficult, but otherwise i can waste quite a lot of time googling.
Thanks,
Urmas.
Editing the question.
This helped a little. what i did now, i changed index.html.erb to
<h1>Listing dates</h1>
<table>
<tr>
<th>Date To</th>
<th></th>
</tr>
<% dates = Array.new %>
<% #tasks.each do |task| %>
<% if ( !dates.include?(task.dateto.strftime("%m.%d.%Y")) ) then
dates.push task.dateto.strftime("%m.%d.%Y")
%>
<tr>
<td><%= task.datefrom.strftime("%m.%d.%Y") %></td>
<td><%= link_to 'Show Date', {:action => "index", :date => task.dateto} %></td> <-- This does not work
</tr>
<%
end
%>
<% end %>
</table>
<br />
<%= link_to 'New Task', new_task_path %>
Where 'Show Date' link link should be made to show_date.html.erb, where is correct code for showing records when input date is passed.
I had added in controller also method
def show_date
#tasks = Task.find(params[:dateto])
#dateto = :dateto
respond_to do |format|
format.html # showdate.html.erb
format.json { render :json => #tasks }
end
end
that can be used in the not working link, to pass data to the 'Show Date' (show_date.html.erb), but i get errors all the time. The problem is with correct calling off the show_date.html.erb, code i will be able to write myself :)
Urmas
in Tasks controller:
add a method
def index_date
#tasks = Task.all
end
and create a index_date.html.erb write
<% #tasks.each do |task| %>
<tr> <td><%= task.datefrom %></td>
<td><%= link_to 'Edit', {:action => "index", :date => task.datefrom} %></td>
</tr>
<%end%>
and in the index method in the controller change
#tasks = Task.all to
#tasks = Task.find_all_by_datefrom(#{params[:date])
This will list the tasks that match the date selected and everything will be similar as it is now thereafter.
Found the solution.
It were necessary to add to routes.rb
resources :tasks do
member do
get 'showdate'
end
get "home/index"
end
add corresponding controller processing
def showdate
#task = Task.find(params[:id])
#tasks = Task.find(:all)
respond_to do |format|
format.html # showdate.html.erb
format.json { render :json => #tasks }
end
end
and to call it using (index.html.erb)
<td><%= link_to 'Show Date', showdate_path(task.id) %></td>
All further processing is already in showdate.html.erb.
Hope this helps somebody.
I AM THE BEST!!!! xD

Edit Multiple (reference Railscasts Episode #165)

I'm following Railscasts Episode #165 Edit Multiple but having issue where when I go to the product index page it is not showing my list of products. I'm getting only the header and the link to new product. Any idea what I did wrong?
By the way I'm using rails 3.2.3
Thank you.
routes.rb
resources :products do
collection do
post :edit_multiple
put :update_multiple
end
end
resources :categories
index.html.erb
<h1>Listing products</h1>
<% form_tag edit_multiple_products_path do %>
<table>
<tr>
<th></th>
<th>Name</th>
<th>Category</th>
<th>Price</th>
</tr>
<% for product in #products %>
<tr>
<td><%= check_box_tag "product_ids[]", product.id %></td>
<td><%= product.name %></td>
<td><%= product.category.name %></td>
<td><%= product.price %></td>
<td><%= link_to "Edit", edit_product_path(product) %></td>
<td><%= link_to "Destroy", product, :confirm => 'Are you sure?', :method => :delete %></td>
</tr>
<% end %>
</table>
<%= submit_tag "Edit Checked" %>
<% end %>
<p><%= link_to "New Product", new_product_path %></p>
You need <%= form_tag ... instead of <% form_tag .... Without the = the return value (i.e. your form) is discarded rather than added to the output.
I think this is typo.
you forgot to use "=" You should do <%= form_tag edit_multiple_products_path do %>

Resources