ruby on rails - why it keep saying undefined method -
problem undefined method"title" #category
but knows parameters:
{"id"=>"1"}
this cloth.rb
class cloth < activerecord::base belongs_to :category end
this category.rb
class category < activerecord::base has_many :cloth end
this category.html.erb problem @ line 16 = <%=@categories.title%>
<div class="col-md-9"> <div class="dreamcrub"> <ul class="breadcrumbs"> <li class="home"> <a href="index.html" title="go home page">home</a> <span>></span> </li> <li class="home"> apparel <span>></span> </li> <li> <%=@categories.title%> </li> </ul> <ul class="previous"> <li><a href="index.html">back previous page</a></li> </ul> <div class="clearfix"></div> </div>
this cloths_controller.rb
class clothscontroller < applicationcontroller before_action :set_cloth, only: [:show, :edit, :update, :destroy] def category @cloths = cloth.all @categories = category.all end # /cloths # /cloths.json def index @cloths = cloth.all @categories = category.all @cloths = cloth.paginate(:page => params[:page], :per_page => 3) end # /cloths/1 # /cloths/1.json def show @cloths = cloth.all @categories = category.all @comments = comment.where("cloth_id = ?", @cloth.id) @comments = comment.paginate(:page => params[:page], :per_page => 3) end #private private # use callbacks share common setup or constraints between actions. def set_cloth @cloth = cloth.find(params[:id]) end # never trust parameters scary internet, allow white list through. def cloth_params params.require(:cloth).permit(:title, :category_id, :description) end def comment_params params.require(:comment).permit(:author,:email,:message) end def category_params params.require(:category).permit(:title,:id,:description) end end
there error in category model. association should like, has_many :cloths besides this, @categories array must have iterate on @categories array title of each category given below: <div class="col-md-9"> <div class="dreamcrub"> <ul class="breadcrumbs"> <li class="home"> <a href="index.html" title="go home page">home</a> <span>></span> </li> <li class="home"> apparel <span>></span> </li> <li> <% @categories.each |category| %> <%= category.title %> <% end %> </li> </ul> <ul class="previous"> <li><a href="index.html">back previous page</a></li> </ul> <div class="clearfix"></div> </div>
Comments
Post a Comment