How can I solve NoMethodError from from ROR? - ruby

Can anybody please help me to resolve the below error.
Error:
NoMethodError in Homes#managebooks
Showing C:/Site/library_management1/app/views/homes/managebooks.html.erb where line #48 raised:
undefined method `book_name' for #<Array:0x229d340>
Actually I want fetch all data to the existing table to display after submitting.When i clicked on submit button all the data saved into DB but after that it is throwing the above error.Please help to resolve those error very soon.
My codes are as follows.
views/homes/managebooks.html.erb
<% if admin_signed_in? %>
<div class="bar">
Logged in as <strong><%= current_admin.email %></strong>.
<%= link_to 'Edit profile', edit_admin_registration_path, :class => 'navbar-link' %> |
<%= link_to "Logout", destroy_admin_session_path, method: :delete, :class => 'navbar-link' %>
</div>
<% else %>
<%= link_to "Sign up", new_admin_registration_path, :class => 'navbar-link' %> |
<%= link_to "Login", new_admin_session_path, :class => 'navbar-link' %>
<% end %>
<div class="big-container">
<% if flash[:color]== "valid" %>
<div class="valid_notice">
<p><%= flash[:notice]%></p>
</div>
<% elsif flash[:color]== "invalid"%>
<div class="invalid_notice">
<p><%=flash[:notice]%></p>
</div>
<%else%>
<div class="notice">
<p><%=flash[:notice]%></p>
</div>
<%end%>
<div class="admin-image">
<div class="bpaddingdiv1"><img src="/assets/admin.png" border="0" name="admin" /></div>
</div>
<div class="borderlightgreen"></div>
<div class="admin-name">
<div class="tpaddingdiv2 textaligncenterdiv"><img src="/assets/adminpanel.png" border="0" name="admin" /></div>
</div>
<div class="leftside">
<div id="leftsidebtn">
<ul>
<li>Manage Books</li>
<li><a href="#" >Manage Pages</a></li>
<li><a href="#" >Manage Header Banner</a></li>
<li><a href="#" >Brand</a></li>
</ul>
</div>
</div>
<div class="middlebox">
<center>
<%= form_for :books,:url => {:action => "savebooks"} do |f| %>
<fieldset>
<p>
<label for="name">Book name</label>
<%= f.text_field :book_name,placeholder:"enter book name" %>
</p>
<p>
<label for="tel">Book title</label>
<%= f.text_field :book_title,placeholder:"enter book title" %>
</p>
<p>
<label for="email">Author Name</label>
<%= f.text_field :author_name,placeholder:"enter book author name" %>
</p>
<p>
<label for="password">publisher Name</label>
<%= f.text_field :publisher_name,placeholder:"enter book publisher name" %>
</p>
<p>
<label for="password"> Edition</label>
<%= f.text_field :edition,placeholder:"enter book edition" %>
</p>
<p>
<label for="password">Pages</label>
<%= f.text_field :pages,placeholder:"enter book pages" %>
</p>
<p>
<label for="password"> Date of pursase</label>
<%= f.date_select :date_of_purchase %>
</p>
<p>
<%= f.submit "SUBMIT" %>
</p>
</fieldset>
<% end %>
</center>
<% if params[:id] %>
<table>
<tr>
<th>Book name</th>
<th>Book title</th>
<th>Author Name</th>
<th>publisher Name </th>
<th>Edition</th>
<th>Pages</th>
<th>Date of pursase</th>
<th>Status</th>
<th>Action</th>
</tr>
<tr>
<td><%= #books.book_name %></td>
<td><%= #books.book_title %></td>
<td><%= #books.author_name %></td>
<td><%= #books.publisher_name %></td>
<td><%= #books.edition %></td>
<td><%= #books.pages %></td>
<td><%= #books.date_of_purchase %></td>
<td><%= image_tag("/assets/1.png") %></td>
<td><%= link_to(
image_tag(
"/assets/logout.png",
:alt => "image", :width => 40, :height => 40, :title => "Delete item"
),
homes_remove_path(:id => #books.id),
:method => :delete, :confirm => "Are you sure to remove this?") %>
</td>
</tr>
</table>
<% end %>
</div>
</div>
controller/homes_controller.rb
class HomesController < ApplicationController
before_filter :authenticate_admin!,only: [:admin]
def index
end
def admin
end
def managebooks
#books=Book.new
if params[:id]
#books=Book.all
end
end
def savebooks
#books=Book.new(params[:books])
if #books.save
flash[:notice]="Data has submitted successfully"
flash[:color]="valid"
redirect_to :action => 'managebooks',:id => #books.id
else
flash[:notice]="Data couldnot submitted successfully"
flash[:color]="invalid"
render 'managebooks'
end
end
def remove
#books=Book.find(params[:id])
#books.destroy
end
end

Change this section of your erb code:
<tr>
<td><%= #books.book_name %></td>
<td><%= #books.book_title %></td>
<td><%= #books.author_name %></td>
<td><%= #books.publisher_name %></td>
<td><%= #books.edition %></td>
<td><%= #books.pages %></td>
<td><%= #books.date_of_purchase %></td>
<td><%= image_tag("/assets/1.png") %></td>
<td><%= link_to(
image_tag(
"/assets/logout.png",
:alt => "image", :width => 40, :height => 40, :title => "Delete item"
),
homes_remove_path(:id => #books.id),
:method => :delete, :confirm => "Are you sure to remove this?") %>
</td>
</tr>
to this:
<% #books.each do |book| %>
<tr>
<td><%= book.book_name %></td>
<td><%= book.book_title %></td>
<td><%= book.author_name %></td>
<td><%= book.publisher_name %></td>
<td><%= book.edition %></td>
<td><%= book.pages %></td>
<td><%= book.date_of_purchase %></td>
<td><%= image_tag("/assets/1.png") %></td>
<td><%= link_to(
image_tag(
"/assets/logout.png",
:alt => "image", :width => 40, :height => 40, :title => "Delete item"
),
homes_remove_path(:id => book.id),
:method => :delete, :confirm => "Are you sure to remove this?") %>
</td>
</tr>
<% end %>
If you look at your method managebooks in homes controller then it creates an array of Book objects if :id is present in params.
NOTE: The above change will again fail since we're using each method on #books, which is not defined for a new Book object if there's no :id in params.

Related

not able to print date in Month_name-yyyy format in rails

hi I am taking date input in mm-yyyy format I want to print date in format of month_name-yyyy format. i have used strftime but i am not to able print the date as my required format.
index.html.erb code
<td><%= project_site.name.titleize %></td>
<td><%= project_site.created_at.strftime('%b-%Y') %></td>
<td><%= link_to ' View attendance', project_site.file, :class => "fi-page-export-csv" %></td>
<% project_site.manager_remarks.each do |manager_remark| %>
<% if manager_remark.decision == false %>
<td><%= 'Rejected' %></td>
<% elsif manager_remark.decision == true %>
<td><%= "Approved" %></td>
<% else %>
<td><%= "Pending" %>
<% end %>
<% end %>
<td><%= project_site.attendance_month.strftime('%b %Y') %></td>
<td><%= link_to 'Remark ', project_site %><span>(<%= project_site.manager_remarks.size %>)</span></td>
<td><%= link_to 'Edit', edit_project_site_path(project_site) %></td>
<td><%= link_to 'Delete', project_site, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
form.html.erb code
<%= form.label :name %>
<%= form.text_field :name %>
</div>
<!--
<div class="field medium-3 columns">
<%= form.label :date %>
<%= form.text_field :date, class: 'datepicker' %>
</div>
-->
<div class="field medium-3 columns">
<%= form.label :upload_attendance %>
<%= form.file_field :file, :class=> 'attendance-file' %>
</div>
<div class="field medium-6 columns">
<%= form.label :attendance_month %>
<%= form.date_select :attendance_month, { :discard_day => true, :discard_month => false, :discard_year => false },:class => 'datetime' %>
</div>
https://www.foragoodstrftime.com/ should help you out
Try:
<td><%= project_site.attendance_month.strftime('%B %Y') %></td>
%B denotes to full name of month in strftime.
Try this code snippet.
<td><%= project_site.created_at.strftime('%B-%Y') %></td>

How to use a checkbox to modify a value from each object of a model using a nested form

I'm new at programming and I have a question related to making a form in Ruby on Rails. I'm making a database for storing products' units. These units are, upon creation, initially located at the storage. This location is stored in a column Unit model. Then I have a model Store and a model Remission.
class Unit < ActiveRecord::Base
belongs_to :store
belongs_to :remission
attr_accessor :into_remission
end
class Remission < ActiveRecord::Base
belongs_to :store
has_many :units
accepts_nested_attributes_for :units
end
class Store < ActiveRecord::Base
has_many :units
has_many :remissions
end
Store has_many :remissions and Remission has_many :units. The idea is that when I put some units for selling in a store I have to create a remission. This remission is the list of products that I gave the store so we both can have a reference of which products are at the store. So I have a form to create a remission in which you can select the store that would change the store_id(reference) of the Remission. I also want to select from this same form the units that would take part in this new remission changing the remission_id(reference) from each unit. For that I made a attar_accessor :into_remissions in the Unit model so I could change that to true in each unit that would end up in the remission with a checkbox. I having a lot of problems making this work, here is the code of my form:
<div class="row">
<div class="col-md-6 col-md-offset-3">
<%= form_for(#remission) do |f| %>
<%= render 'shared/error_messages_remissions' %>
#Here you select the store that would own the remission and the products
<div class="field">
<%= f.label :store_id, "Producto" %> <br/>
<%= f.collection_select :store_id, #stores.order(:name), :id, :name, prompt: "Select Store" %>
</div>
#Here its a dynamic table that display all the products in storage
<table id="products" class="table table-striped table-bordered" cellspacing="0" width="100%">
<thead>
<tr>
<th>Product Name</th>
<th>Remission</th>
</tr>
</thead>
<tbody>
#HERE I WANT TO MAKE A CHECK_BOX THAT CHANGES THE INTO_REMISSION VALUE FROM EACH UNIT. SO IT MARKS THE UNITS THAT WOULD TAKE PART IN THE REMISSION
<%= f.fields_for :units do |ff| %>
<% #units.each do |unit| %>
<% unless unit.sold %>
<tr>
<td><%= unit.product.name %></td>
<td>
<%= ff.label :into_remission, "Nombre de tu Marca:" %>
<%= ff.check_box :into_remission%>
</td>
</tr>
<% end %>
<% end %>
<% end %>
</tbody>
</table>
<%= f.submit "Submit", class: "btn btn-primary" %>
<% end %>
</div>
</div>
Needless to say he check_box its now showing nor working.
I not sure how to make this work and any advices would be welcome as I'm new with rails and programming. Thanks
Try changing this:
<%= f.fields_for :units do |ff| %>
<% #units.each do |unit| %>
<% unless unit.sold %>
<tr>
<td><%= unit.product.name %></td>
<td>
<%= ff.label :into_remission, "Nombre de tu Marca:" %>
<%= ff.check_box :into_remission%>
</td>
</tr>
<% end %>
<% end %>
<% end %>
into this:
<% #units.each do |unit| %>
<%= f.fields_for :units, unit do |ff| %>
<% unless unit.sold %>
<tr>
<td><%= unit.product.name %></td>
<td>
<%= ff.label :into_remission, "Nombre de tu Marca:" %>
<%= ff.check_box :into_remission%>
</td>
</tr>
<% end %>
<% end %>
<% end %>
I think you should first iterate over the units and pass each unit separately into the fields for. That should solve the showing issue.

Strong Params Unpermitted parameters

I know there are alot of questions on this, but after looking at about a dozen or so none of them appear to be the same problem. I have an "Action" model that I am just simply trying to edit and update. (fyi, this isn't a nested form, I'm not using devise, and this isn't an activeadmin problem...like virtually all other questions about this topic are). I'm getting
error:
unpermitted parameters: utf8, _method, authenticity_token when I do this.
Action params in Actions_controller:
def action_params
params.permit(:id, :call_answered, :is_customer, :category_id, :actiontype_id, :why, :reviewer_id, :reviewed, :spam, :lead)
end
IF I change it to:
params.require(:action).permit(:id, :call_answered, :is_customer, :category_id, :actiontype_id, :why, :reviewer_id, :reviewed, :spam, :lead)
end
than I get the below error (and I can't find any solution for this):
undefined method `permit' for "update":String
Actions Controller:
def edit
#action = Action.find(params[:id])
#actiontypes = Actiontype.all
#categories = Category.all
#lead_categories = #categories.where(lead: true)
#user = current_user
#reviewer = User.find(#action.reviewer_id) if #action.reviewed?
end
def update
#action = Action.find(params[:id])
if #action.update!(action_params)
binding.pry
flash[:success] = "Loop closed!"
redirect_to '/closingloop/actions?reviewed=false'
else
flash[:danger] = "Something Went Wrong! The loop was not closed!"
render :edit
end
end
def action_params
params.permit(:id, :call_answered, :is_customer, :category_id, :actiontype_id, :why, :reviewer_id, :reviewed, :spam, :lead)
end
View:
<%= form_for [:closingloop, #action] do |f| %>
<%= f.hidden_field :reviewer_id, :value => #user.id %>
<%= f.hidden_field :reviewed, :value => true %>
<table width="70%" align="center" class="table table-responsive">
<tr>
<th>Tracking Number</th>
<td><%= #action.company.tracking_phone_number %></td>
</tr>
<tr>
<th>Target Number</th>
<td><%= #action.company.phonenumber %></td>
</tr>
<tr>
<th>Opportunity Name</th>
<td><%= #action.opportunity_name %></td>
</tr>
<tr>
<th>Opportunity Address</th>
<td><%= #action.customer_address %></td>
</tr>
<tr>
<th>Opportunity Phone</th>
<td><%= #action.caller_phone_number %></td>
</tr>
<tr>
<th>Call Recording</th>
<td><%= audio_tag(#action.call_recording_link, controls: true) %></td>
</tr>
<tr>
<th>Duration</th>
<td><%= #action.duration %></td>
</tr>
<tr>
<th>Call Status</th>
<td><%= #action.call_status %></td>
</tr>
<tr>
<th>Date & Time</th>
<td><%= #action.created_at.to_formatted_s(:long) %></td>
</tr>
<% if #action.reviewed? %>
<tr id="row_reviewed_by">
<th>Reviewed By</th>
<td><%= #reviewer.first_name %> <%= #reviewer.last_name %></td>
</tr>
<% end %>
<tr><td colspan="2"> </td></tr>
<tr id="q_call_answered">
<th>Call Answered?</th>
<td>
<div class="radio-toolbar">
<%= f.radio_button :call_answered, true, class: 'call_answered_true' %>
<%= f.label :call_answered, "Yes" %>
<%= f.radio_button :call_answered, false, class: 'call_answered_false' %>
<%= f.label :call_answered, "No" %>
</div>
</td>
</tr>
<tr id="why_not_answered" style="display:none;">
<th>Why wasn't it answered?</th>
<td>
<%= f.select :why, options_for_select([["No Answer", "N"], ["Abandoned", "A"], ["After Business Hours", "H"]], #action.why), { include_blank: true } %>
</td>
</tr>
<tr id="q_opportunity" style="display:none;">
<th>Was it a opportunity?</th>
<td>
<div class="radio-toolbar">
<%= f.radio_button :is_customer, true, class: 'opportunity_true' %>
<%= f.label :is_customer, "Yes" %>
<%= f.radio_button :is_customer, false, class: 'opportunity_false' %>
<%= f.label :is_customer, "No" %>
</div>
</td>
</tr>
<tr id="opportunity_type" style="display:none;">
<th>Opportunity Type</th>
<td>
<%= f.select :actiontype_id, options_from_collection_for_select(#actiontypes, 'id', 'action_type', #action.actiontype_id), { include_blank: true } %>
</td>
</tr>
<tr id="reason_for_call" style="display:none;">
<th>Reason for Call</th>
<td><%= f.select :category_id, options_from_collection_for_select(#categories, 'id', 'reason', #action.category_id), { include_blank: true } %></td>
</tr>
<tr>
<td> </td>
<td><input type="submit" value="Save" id="save" class="btn btn-success" /></td>
</tr>
</table>
<% end %>
The main problem here is that if you have model named 'action', your params by default will be named in the same way, which collides with ActionController default params. The simplest solution is, I guess, renaming your model.

Rails 3 - Partial Not Rendering

In my view I have the following:
<div class="subject edit">
<%= form_for(:subject, :url => {:action => 'update', :id => #subject.id}) do |f| %>
<%= render(:partial => "form", :locals => {:f => f}) %>
<div class="form-buttons">
<%= submit_tag("Update Subject") %>
</div>
<% end %>
In the same folder I have created a partial (_form.html.erb). But for some reason it is not rendered.
_form.html.erb:
<table summary="Subject form fields">
<tr>
<th>Name</th>
<td><%= f.text_field(:name) %></td>
</tr>
<tr>
<th>Position</th>
<td><%= f.text_field(:position) %></td>
</tr>
<tr>
<th>Visible?</th>
<td><%= f.text_field(:visible) %></td>
</tr>
I'm running rails 3.2.7. Can some see what I'm doing wrong?
Thanks!
A server reset fixed the issue. Doh!

How to make ajax editable cell?

I have this index.html.erb file:
<h3>Pažymiai</h3><br />
Nuo: <%= #begining_of_week = Date.commercial(2011, 48, 1) %>
iki: <%= #end_of_week = Date.commercial(2011, 48, -1) %>
<table class="macs">
<tr>
<th>Vardas, pavardė</th>
<th>Dalykas</th>
<% (#begining_of_week..#end_of_week).each do |d| %>
<th class="calendar"><%= d.day %></th>
<% end %>
</tr>
<% #students.each do |student| %>
<tr>
<td class="name" rowspan="<%= #subjects.count %>"><%=link_to(admin_student_path(student)) do %><%= student.name%><br /> <%= student.surname %><% end %></td>
<% #subjects.each do |subject| %>
<td class="subject"><%=subject.name%></td>
<% (#begining_of_week..#end_of_week).each do |d| %>
<% #mac = Mac.where(:student_id => student.id, :subject_id => subject.id, :date => d) %>
<% if #mac.blank? %>
<td class="calendar_mac" onclick="location.href='<%= new_admin_mac_path %>'"><center>
</center>
<%= link_to 'new_mac', new_admin_mac_path, :remote => true %>
</td>
<% else %>
<% #mac.each do |mac| %>
<td class="calendar_mac" onclick="location.href='<%= edit_admin_mac_path(mac) %>'"><center>
<%= mac.mac%>
<% end %>
<% end %>
</center></td>
<% end %>
</tr>
<% end %>
<% end %>
</table>
How to make if cell is empty to write new value, if cell has value make it editable, when click cell i want a popup window where you could edit or add new element to database. My table looks:
You can try the best in place gem and follow a sample screen cast by ryanb this might help you out

Resources