我想根据某个需要展示相关产品,只是图片和名称而已,取决于类别这里是mu控制器
class RelatedneedsController < ApplicationController
def index
@relatedneeds = RelatedNeed.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: @relatedneeds }
end
end
def show
s1 = '@need.category.name'
s2 = '@relatedneed.category.name'
if s1.eql?(s2)
@relatedneed = relatedneed.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: @need }
end
end
end
def get_name
@relatedneed.name
end
end
这是我的模型
class Relatedneed
include Mongoid::Document
include Mongoid::Paperclip
mapping do
indexes :name
end
field :name, type: String
belongs_to :category
belongs_to :user
end
这是 show.haml 文件
%h1
%b= @need.name
#container{:style => "width:1000px"}
#desc{:style => "height:400px;width:400px;float:left;"}
=image_tag @relatedneed.photo.url(:normal)
这是我的 index.haml 文件
%h1= @relatedneed.get_name
#container{:style => "width:1000px"}
#desc{:style => "background-color:#EEEEEE;height:400px;width:400px;float:left;"}
= link_to "Check Need", new_need_path
我不知道他们是否缺少某些东西,并且我在 RelatedneedsController#index 中收到此错误 NoMethodError
未定义的方法“键?” 对于零:NilClass