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


i trying implement blog application in app.but getting following error after saving posts.

error:

nomethoderror in homes#blog  showing c:/site/library_management1/app/views/homes/blog.html.erb line #19 raised:  undefined method `name' #<array:0x21896a0> 

actually blog implementation story this.when logged in user add name , comment in required place , clicked on submit button blogs saved display.all comments should visible logged in users belongs app.suppose 1 user wants reply/edit he/she can this.

my codes follows.

views/homes/blog.html.erb:

<% if current_user %> <div class="totaldiv">   <div class="navdiv"><span>student information</span><span>logged in <%= 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 } |f| %>  <p>   <label for="name">name:</label>   <%= f.text_field :name,placeholder:"enter name" %> </p> <p>   <label for="comment">comment:</label>   <%= f.text_area :body,:class => "blog-navigation",placeholder:"type comment here" %> </p> <p>   <%= f.submit "add comments",:class =>"btn btn-success" %> </p>  <% end %>  <% if params[:id] %>  <h1>comments</h1>  <% @blogs.each |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 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 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 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 codes , let me know did mistake , kindly me make blog application correctly according story explained above.

this not forum post random code , have else fix it. please make sure read guide on how ask question: https://stackoverflow.com/help/how-to-ask

as next step please make sure read , understand error messages, because best thing solve problems yourself.

next i'd point blog post explaining how debug rails application yourself: http://nofail.de/2013/10/debugging-rails-applications-in-development/

it's important understand rails error-message trying tell you:

undefined method name array:0x21896a0

so it's calling .name on array.

showing c:/site/library_management1/app/views/homes/blog.html.erb line #19 raised:

it's doing in blog.html.erb on line 19 suppose part here:

 <%= form_for :blogs,:url => {:action => 'savecomments',:id => current_user.id } |f| %>  <p>   <label for="name">name:</label>   <%= f.text_field :name,placeholder:"enter name" %> </p> 

so form trying call .name on nested object defined :blogs in order :name property f.text_field. object form trying work with?

nomethoderror in homes#blog

looking @ controller code of homes#blog see possible form object comes from:

def blog     if params[:id]         @blogs=blog.find(params[:id])         @blogs=blog.all     else     @blogs=blog.new     end end 

so thing array @blogs = blog.all. , code not makes sense me. why doing blog.find replace blog.all in next line?


Comments

Popular posts from this blog

c++ - Delete matches in OpenCV (Keypoints and descriptors) -

java - Could not locate OpenAL library -

sorting - opencl Bitonic sort with 64 bits keys -