Actually I want to display all data of Magazine model on the admin side page But i got the following error while tried to fetch all data on the admin side(i.e.http://localhost:3000/homes/magazineissue).I also want to display some value belongs to User model through user's id.
Error:
NoMethodError in HomesController#magazineissue
undefined method `user_id' for #<Array:0x2780b98>
Rails.root: C:/Site/library_management1
Application Trace | Framework Trace | Full Trace
app/controllers/homes_controller.rb:100:in `magazineissue'
My codes are as follows.
views/homes/magazineissue.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">
<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><a href="/homes/managebooks">Manage Books</a></li>
<li><a href="/homes/userissues" >User Issues</a></li>
<li><a href="/homes/magazineissue" >Magazine Issues</a></li>
</ul>
</div>
</div>
<div class="middlebox">
<center>
<table>
<thead>
<tr>
<th>First Name</th>
<th>Email Id</th>
<th>Magazine Name</th>
<th>Magazine Title</th>
<th>Purchased on</th>
<th>CD/DVD</th>
</tr>
</thead>
<tbody>
<% @magazines.each do |magazine| %>
<tr>
<td><%= @users.first_name %></td>
<td><%= @users.email %></td>
<td><%= magazine.mag_name %></td>
<td><%= magazine.mag_title %></td>
<td><%= magazine.purchased_on %></td>
<td><%= magazine.cd_dvd %></td>
</tr>
<% end %>
</tbody>
</table>
</center>
</div>
</div>
views/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.find(params[:id])
@book=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
def books
end
def showbooks
@books=Book.all
end
def searchbooks
@books=Book.all
end
def member
@users=User.new
end
def registration
@users=User.new
end
def savedata
@users=User.new(params[:users])
if @users.save
flash[:notice]="Data has submitted successfully"
flash[:color]="valid"
redirect_to :action => 'member'
else
flash[:notice]="Data could not submitted successfully"
flash[:color]="invalid"
render 'registration'
end
end
def issuebooks
@issues=Issue.new
end
def savedissuebooks
@issues=Issue.new(params[:issues])
if @issues.save
flash[:notice]="information has saved successfully"
flash[:color]="valid"
redirect_to :action => 'member'
else
flash[:notice]="Data couldnot saved"
flash[:color]="invalid"
render 'issuebooks'
end
end
def availablebooks
@books=Book.all
end
def userissues
@issues=Issue.all
end
def magazine
@magazines=Magazine.new
end
def savemagazines
@users=User.find(params[:id])
@magazines=Magazine.new(params[:magazines])
@magazines.user_id=@users.id
if @magazines.save
flash[:notice]="Data submitted successfully"
flash[:color]="valid"
redirect_to :action => "member"
else
flash[:notice]="Data could not saved"
flash[:color]="invalid"
render 'magazines'
end
end
def magazineissue
@magazines=Magazine.all
@users=User.find(@magazines.user_id)
end
end
db/migrate/20150317084229_create_magazines.rb
class CreateMagazines < ActiveRecord::Migration
def change
create_table :magazines do |t|
t.string :mag_name
t.boolean :cd_dvd
t.decimal :cost, :precision => 8, :scale => 2
t.date :purchased_on
t.string :mag_title
t.integer :user_id
t.timestamps
end
end
end
model/magazine.rb
class Magazine < ActiveRecord::Base
attr_accessible :cd_dvd, :cost, :mag_name, :mag_title, :purchased_on
belongs_to :user
end
model/user.rb
class User < ActiveRecord::Base
attr_accessible :address, :email, :first_name, :last_name, :password, :password_hash, :password_salt, :tel_no ,:password_confirmation
attr_accessor :password
before_save :encrypt_password
EMAIL_REGEX = /\A[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\z/i
validates :email, :presence => true, :uniqueness => true, :format => EMAIL_REGEX
validates :first_name, :presence => true, :length => {:in => 3..10}
validates :last_name , :presence => true , :length => {:in => 3..10}
validates :tel_no , :presence => true , :length => {:in => 1..10}
validates :password, :confirmation => true
validates_length_of :password, :in => 6..20, :on => :create
def self.authenticate(email, password)
user = find_by_email(email)
if user && user.password_hash == BCrypt::Engine.hash_secret(password, user.password_salt)
user
else
nil
end
end
def encrypt_password
if password.present?
self.password_salt = BCrypt::Engine.generate_salt
self.password_hash = BCrypt::Engine.hash_secret(password, password_salt)
end
end
has_many :issue
has_many :book
has_many :magazine
end
Please help me to resolve this error.
Aucun commentaire:
Enregistrer un commentaire