Ruby nil:NilClass的未定义方法“plantposts”
这是我在railstutorial.org上完成教程后的第一个应用程序,在此之前一切都很顺利。在我的应用程序中,每个用户都可以创建植物,每个植物都可以有许多植物贴子。我已经在plants/id show页面上放置了一个表格,用于创建我无法工作的新PlantPost。我怀疑这可能与routes文件有关 这是我的控制器:Ruby nil:NilClass的未定义方法“plantposts”,ruby,ruby-on-rails-4,Ruby,Ruby On Rails 4,这是我在railstutorial.org上完成教程后的第一个应用程序,在此之前一切都很顺利。在我的应用程序中,每个用户都可以创建植物,每个植物都可以有许多植物贴子。我已经在plants/id show页面上放置了一个表格,用于创建我无法工作的新PlantPost。我怀疑这可能与routes文件有关 这是我的控制器: class PlantpostsController < ApplicationController before_action :logged_in_user, only
class PlantpostsController < ApplicationController
before_action :logged_in_user, only: [:create, :destroy]
before_action :correct_user, only: :destroy
def create
#@plantpost = Plantpost.new(plantpost_params)
#@plant = Plant.find(params[:plant_id])
@plantpost = @plant.plantposts.build(plantpost_params) #this seems to be the main problem line.
if @plantpost.save
flash[:success] = "Plantpost created!"
redirect_to plant_path(@plantpost.plant_id)
else
flash[:danger] = "No Plantpost created!"
@plantpost_feed_items = []
redirect_to plant_path(id: params[:plant_id] )
end
end
def destroy
@plantpost.destroy
flash[:success] = "Plantpost deleted"
redirect_to request.referrer || root_url
我的植物控制员
class PlantsController < ApplicationController
before_action :logged_in_user, only: [:index, :create, :edit, :update,
:destroy, :show]
before_action :correct_user, only: [:destroy, :update ]
def new
@plant = Plant.new
end
def index
@plant_feed_items = current_user.plants.paginate(page: params[:page], :per_page => 10)
@plants = current_user.plants.all
@plant = Plant.new
end
def show
@plant = Plant.find(params[:id])
@plantpost_feed_items = @plant.plantpost_feed.paginate(page: params[:page], :per_page => 10)
@plantposts = @plant.plantposts.paginate(page: params[:page])
@plantpost = Plantpost.new
end
private
def plant_params
params.require(:plant).permit( :notes, :plantname,:source, :planted, :harvested, :yield, :yieldunits)
end
def correct_user
@plant = current_user.plants.find_by(id: params[:id])
redirect_to root_url if @plant.nil?
end
def plantpost_params
params.require(:plantpost).permit(:content, :picture, :user_id, :plant_id)
end
结束
从所有的hashtag中可以看出,我尝试了很多组合,希望能找到正确的组合。
我还编写了一个集成测试,在该测试中,这条生产线将发布到工厂_path@plant.id,plantpost:{content:}获取此响应
错误[test_plantpost_接口,PlantpostInterfaceTest,1.515159549]
测试\u plantpost\u接口PlantPostInterfaceTest 1.52s
ActionController::RoutingError:ActionController::RoutingError:No route matches[POST]>/plants/980190962
测试/集成/plantposts_接口_测试。rb:16:块中
'
测试/集成/plantposts_接口_测试。rb:16:inblock in'
看起来你想做的就是把资源嵌套起来。 在路由文件中,更改
resources :plantposts, only: [:create, :destroy]
resources :plants
到
这是一个语法问题。工作创建代码为:
def create
@plant = Plant.find_by(id: params[:plant_id] )
@plantpost = @plant.plantposts.build({:plant_id => params[:plant_id], :user_id => current_user.id, :content => params[:plantpost][:content], :picture => params[:plantpost][:picture]})
if @plantpost.save
flash[:success] = "Plantpost created!"
redirect_to plant_path :id => @plant.id
else
@plantpost_feed_items = []
render 'plants/show', :id => @plant.id
end
end
我相信有一种更干净的方法可以做到这一点,但我会继续前进。谢谢。我更改了routes文件并重新定义了一些命名路径,虽然我的测试进展得稍微快了一点,但我仍然在plantposts控制器中为nil:NilClass错误获取未定义的方法“plantposts”。当然,当我使用IRB控制台时,一切正常,但是当我运行应用程序时,我得到了异常,尽管所有参数都正常。
Rails.application.routes.draw do
get 'password_resets/new'
get 'password_resets/edit'
root 'static_pages#home'
get 'help' => 'static_pages#help'
get 'about' => 'static_pages#about'
get 'contact' => 'static_pages#contact'
get 'signup' => 'users#new'
get 'login' => 'sessions#new'
post 'login' => 'sessions#create'
delete 'logout' => 'sessions#destroy'
resources :users do
member do
get :following, :followers
end
end
resources :account_activations, only: [:edit]
resources :password_resets, only: [:new, :create, :edit, :update]
resources :microposts, only: [:create, :destroy]
resources :relationships, only: [:create, :destroy]
resources :plantposts, only: [:create, :destroy]
resources :plants
#post 'plants' => 'plantposts#create'
resources :plantposts, only: [:create, :destroy]
resources :plants
resources :plants do
resources :plantposts, only: [:create, :destroy]
end
def create
@plant = Plant.find_by(id: params[:plant_id] )
@plantpost = @plant.plantposts.build({:plant_id => params[:plant_id], :user_id => current_user.id, :content => params[:plantpost][:content], :picture => params[:plantpost][:picture]})
if @plantpost.save
flash[:success] = "Plantpost created!"
redirect_to plant_path :id => @plant.id
else
@plantpost_feed_items = []
render 'plants/show', :id => @plant.id
end
end