vendredi 20 mars 2015

How to display all posts in a blog application using Rails 3?

I am trying to implement a blog application in my app.But i am getting the following error after saving the posts.


Error:



NoMethodError in Homes#blog

Showing C:/Site/library_management1/app/views/homes/blog.html.erb where line #19 raised:

undefined method `name' for #<Array:0x21896a0>


Actually My blog implementation story is like this.When a logged in user will add his name and comment in required place and clicked on submit button all blogs will saved as well as display.All comments should visible to all logged in users belongs to this app.Suppose one user wants to reply/edit then he/she can do this.


My codes are as follows.


views/homes/blog.html.erb:



<% if current_user %>
<div class="totaldiv">
<div class="navdiv"><span>STUDENT INFORMATION</span><span>Logged in as <%= current_user.email %></span></div>
<div class="wrapper">
<div id="leftsidebtn">
<ul>
<li><a href="/homes/issuebooks">Book issue</a></li>
<li><a href="/homes/availablebooks">Books Available</a></li>
<li><a href="/homes/magazines?user_id=<%= current_user.id %>">Magazines Purchase</a></li>
<li><a href="/sessions/removeuser">Log Out</a></li>
</ul>
</div>
</div>
<div class="restdiv" id="ex3" >
<center>
<%= form_for :blogs,:url => {:action => 'savecomments',:id => current_user.id } do |f| %>
<p>
<label for="name">Name:</label>
<%= f.text_field :name,placeholder:"Enter your name" %>
</p>
<p>
<label for="Comment">Comment:</label>
<%= f.text_area :body,:class => "blog-navigation",placeholder:"Type your comment here" %>
</p>
<p>
<%= f.submit "Add Comments",:class =>"btn btn-success" %>
</p>
<% end %>
<% if params[:id] %>
<h1>Comments</h1>
<% @blogs.each do |blog| %>
<div class="blog-site">
<div class="name-site">
<%= blog.name %>
</div>
<div class="message-site">
<%= blog.body %>
</div>
<div class="reply-site">
<%= link_to 'Replay',homes_blog_path %>
</div>
</div>
<% end %>
<% end %>
</center>
</div>
</div>
<% end %>


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.first.user_id
end
def blog
if params[:id]
@blogs=Blog.find(params[:id])
@blogs=Blog.all
else
@blogs=Blog.new
end
end
def savecomments
@users=User.find(params[:id])
@blogs=Blog.new(params[:blogs])
@blogs.user_id=@users.id
if @blogs.save
flash[:notice]="Comment has been posted successfully"
flash[:color]="valid"
redirect_to :action => "blog",:id => params[:id]
else
flash[:notice]="Comment could not saved"
flash[:color]="invalid"
render 'blog'
end
end
end


model/blog.rb



class Blog < ActiveRecord::Base
attr_accessible :body, :name
validates :name, :presence => true, :length => {:in => 3..10}
belongs_to :user
end


Please check my all codes and let me to know where i did the mistake and kindly help me to make this blog application correctly according to my story which is explained above.


Aucun commentaire:

Enregistrer un commentaire