What is error with cancan in view - ruby-on-rails

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?

Related

Error when delete comment Ruby on Rails

When I delete comment I see
undefined method 'each' for nil:NilClass
Also, the user cannot delete his account if has posted... It makes the error.
This is code
<p id="notice"><%= notice %></p>
<h1>Comments</h1>
<table>
<thead>
<tr>
<th>Link</th>
<th>Body</th>
<th>User</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% #comments.each do |comment| %>
<tr>
<td><%= comment.link_id %></td>
<td><%= comment.body %></td>
<td><%= comment.user %></td>
<td><%= link_to 'Show', comment %></td>
<td><%= link_to 'Edit', edit_comment_path(comment) %></td>
<td><%= link_to 'Destroy', comment, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<%= link_to 'New Comment', new_comment_path %>
Your are not checking if any comments are present before looping through them. So if there are no comments you are calling each on nil.
This should fix it
<tbody>
<% if #comments.any? %> <----- Add in this line and the end below
<% #comments.each do |comment| %>
<tr>
<td><%= comment.link_id %></td>
<td><%= comment.body %></td>
<td><%= comment.user %></td>
<td><%= link_to 'Show', comment %></td>
<td><%= link_to 'Edit', edit_comment_path(comment) %></td>
<td><%= link_to 'Destroy', comment, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
<% end %> <---- here
</tbody>

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)

Undefined method Error message in Ruby 2.1

I have zero experience with Ruby, and I'm having the following issue:
I receive a "We're sorry, but something went wrong" error message when I try to login into my admin panel (mydomain.com/administrator).
Checking the logs I found out the following:
As well, I checked the login_controller.rb:
Here you have the template:
<h1><%= t(".title")%></h1>
<!-- Pages -->
<% if #pages.length > 0 %>
<h3><%= t(".lastets_pages")%></h3>
<table>
<thead>
<tr>
<th>#</th>
<th><%=t("activerecord.attributes.page.title")%></th>
<th><%=t("activerecord.attributes.page.slug")%></th>
<th><%=t("generic.user_id")%></th>
<th><%=t("generic.created_at")%></th>
<th><%=t("generic.updated_at")%></th>
<th><%=t("generic.actions")%></th>
</tr>
</thead>
<tbody>
<% #pages.each do |page| %>
<tr>
<td><%= page.id %></td>
<td><%= page.title %></td>
<td><%= page.slug %></td>
<td><%= page.user.username %></td>
<td><%= l(page.created_at, format: :long) %></td>
<td><%= l(page.updated_at, format: :long) %></td>
<td class="actions_links">
<%= link_to t("generic.edit"), edit_administrator_page_path(page) %>
<%= link_to t("generic.delete"), administrator_page_path(page), :confirm => t("generic.delete_confirmation"), :method => :delete, class: "delete" %>
</td>
</tr>
<% end %>
</tbody>
</table>
<% else %>
<h3><%= t(".lastets_pages")%></h3>
<div class="alert alert-warning"> <%= t(".lastets_pages_empty")%> </div>
<% end %>
<!-- Notices -->
<% if #notices.length > 0 %>
<h3><%= t(".lastets_notices")%></h3>
<table>
<thead>
<tr>
<th>#</th>
<th><%=t("activerecord.attributes.notice.title")%></th>
<th><%=t("activerecord.attributes.notice.countries")%></th>
<th><%=t("activerecord.attributes.notice.slug")%></th>
<th><%=t("generic.user_id")%></th>
<th><%=t("generic.created_at")%></th>
<th><%=t("generic.updated_at")%></th>
<th><%=t("generic.actions")%></th>
</tr>
</thead>
<tbody>
<% #notices.each do |notice| %>
<tr>
<td><%= notice.id %></td>
<td><%= notice.title %></td>
<td><%= notice.show_countries %></td>
<td><%= notice.slug %></td>
<td><%= notice.user.username %></td>
<td><%= l(notice.created_at, format: :long) %></td>
<td><%= l(notice.updated_at, format: :long) %></td>
<td class="actions_links">
<%= link_to t("generic.edit"), edit_administrator_notice_path(notice) %>
<%= link_to t("generic.delete"), administrator_notice_path(notice), :confirm => t("generic.delete_confirmation"), :method => :delete, class: "delete" %>
</td>
</tr>
<% end %>
</tbody>
</table>
<% else %>
<h3><%= t(".lastets_notices")%></h3>
<div class="alert alert-warning"> <%= t(".lastets_notices_empty")%> </div>
<% end %>
<!-- Faqs -->
<% if #faqs.length > 0 %>
<h3><%= t(".lastets_faqs")%></h3>
<table>
<thead>
<tr>
<th>#</th>
<th><%=t("activerecord.attributes.faq.title")%></th>
<th><%=t("activerecord.attributes.faq.slug")%></th>
<th><%=t("generic.user_id")%></th>
<th><%=t("generic.created_at")%></th>
<th><%=t("generic.updated_at")%></th>
<th><%=t("generic.actions")%></th>
</tr>
</thead>
<tbody>
<% #faqs.each do |faq| %>
<tr>
<td><%= faq.id %></td>
<td><%= faq.title %></td>
<td><%= faq.slug %></td>
<td><%= faq.user.username %></td>
<td><%= l(faq.created_at, format: :long) %></td>
<td><%= l(faq.updated_at, format: :long) %></td>
<td class="actions_links">
<%= link_to t("generic.edit"), edit_administrator_faq_path(faq) %>
<%= link_to t("generic.delete"), administrator_faq_path(faq), :confirm => t("generic.delete_confirmation"), :method => :delete, class: "delete" %>
</td>
</tr>
<% end %>
</tbody>
</table>
<% else %>
<h3><%= t(".lastets_faqs")%></h3>
<div class="alert alert-warning"> <%= t(".lastets_faqs_empty")%> </div>
<% end %>
Notice.rb model:
Can you help me to identify the issue? I tried to clear the cache and nothing happened.
The error backtrace points to the notice.rb:7 file, which is the line inside the block:
countries << country_post.country.name
Undefined method 'name' for nil:NilClass means you are trying to call nil.name, so for a given country_post, country_post.country returns nil.
You'll have to check this part of the code to solve the bug. Another thing you can do is use the try method:
countries << country_post.country.try(:name)
this try method will return country.name if country is not nil, ornil` otherwise:
https://github.com/rails/rails/blob/cd2d3664e3b434d15b6c19e652befb386187642f/activesupport/lib/active_support/core_ext/object/try.rb#L93
https://github.com/rails/rails/blob/cd2d3664e3b434d15b6c19e652befb386187642f/activesupport/lib/active_support/core_ext/object/try.rb#L62
In your Notice.rb model use:
countries << country_post.country.try(:name)
It seems that you are trying to use the notice.show_countries method.
The log is telling you that the method name does not exist for nil, which means that your object is nil.
Please show us the content of the "show_countries" method and please check that you have an acutal object given to the line of code responsible for the method name call.
Have you tried using a debugger ?
UPDATE:
Based on the notice code, your problem is that some post, does not have a coutry, then, country_post.country is nil which result to your error.
Either you ensure that you have a default country at the creation, or you do it in two step when trying to retrieve the information
if country_post.country.blank?
countries << 'DEFAULT_COUNTRY_NAME'
else
country << country_post.country.name
The answer by #mrcasals most directly answers the question and will result in that exception no longer being raised.
However, a more "fundamental" problem here is that you are expecting some data to be persisted to the database which is not. Rails' canonical solution to the problem of ensuring the existence of attributes is via ActiveRecord validations. The general overview is a great read, and the presence validation should do the trick for this problem in particular.

will_paginate (getting undefined method error for boards)

Maybe this is a newbie issue but I just can't figure out was going on
Getting an error when I refresh local host
undefined local variable or method `boards' for #<#:0x3c8eff8>
Extracted source (around line #14):
Here is the code in my index.html.erb for boards
Listing boards
<table>
<thead>
<tr>
<th>Name</th>
<th>About</th>
<th>User</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<%= will_paginate(boards) %>
<% #boards.each do |board| %>
<tr>
<td><%= board.name %></td>
<td><%= board.about %></td>
<td><%= board.user.full_name %></td>
<td><%= link_to 'Show', board %></td>
<td><%= link_to 'Edit', edit_board_path(board) %></td>
<td><%= link_to 'Destroy', board, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<%= link_to 'New Board', new_board_path %>
****Here is the code for my index method in board controller****
def index
if user_signed_in?
#boards = current_user.boards
else
#boards = Board.paginate(page: params[:page], per_page: 1)
end
end
I have added the will_paginate to my gem file
source 'https://rubygems.org'
gem 'will_paginate'

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

Resources