浏览器在Locals中显示以下错误#show:
用于NilClass的未定义方法’model_name’:Class
51: <%= form_for(@food) do |f| %> 52: <%= render 'shared/error_messages',object: f.object %> 53: <div class="field"> 54: <%= f.label :nome %>
这是我的locals_controller.rb(show动作)
def show @local = Local.find(params[:id]) @foods = @local.foods.paginate(page: params[:page]) respond_to do |format| format.html # show.html.erb format.json { render json: @local } end
结束
还有这里的foods_controller.rb(创建动作)
def create @food = @local.foods.build(params[:food]) if @food.save flash[:success] = "Food created!" redirect_to '/locals' else flash[:error] = "Error on creating food" render '/locals' end end
食物模型和本地模型与:has_many和belongs_to有关
有什么问题?谢谢
解决方法
然后将您的视图代码更改为:
<%= form_for :food,:url => {:action => :create} do |f| %>