这是我在railstutorial.org上完成教程后的第一个应用程序,一切都很顺利。在我的应用程序中,每个用户都可以创建植物,每个植物可以有许多植物。我已经在植物/ id(显示)页面上放置了一个表单来创建新的植物插页,这是我无法工作的。我怀疑它可能与路径文件有关。
这是我的控制器:
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
端
私有
def plantpost_params
params.require(:plantpost).permit(:content, :picture, :user_id, :plant_id)
end
def correct_user
@plantpost = current_user.plantposts.find_by(id: params[:id])
redirect_to root_url if @plantpost.nil?
end
end
我的植物控制器
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
模型关系的相关部分
class User < ActiveRecord::Base
has_many :plants, dependent: :destroy
has_many :plantposts, through: :plants, dependent: :destroy
class Plant < ActiveRecord::Base
has_many :plantposts, dependent: :destroy
belongs_to :user
class Plantpost < ActiveRecord::Base
belongs_to :plant
belongs_to :user
路线档案
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'
端
正如你从所有标签中看到的那样,我一直在尝试很多希望偶然发现正确标签的组合。 我还编写了一个集成测试,其中“post plant_path(@ plant.id),plantpost:{content:”“}”获得此回复
ERROR [“test_plantpost_interface”,PlantpostInterfaceTest,1.515159549] test_plantpost_interface #PlantpostInterfaceTest(1.52s) ActionController :: RoutingError:ActionController :: RoutingError:没有路由匹配[POST]&gt;“/ plants / 980190962” test / integration / plantposts_interface_test.rb:16:in
block in <class:PlantpostInterfaceTest>' test/integration/plantposts_interface_test.rb:16:in
块中的'
答案 0 :(得分:0)
看起来你想要做的就是嵌套资源。 在路线文件中,更改
resources :plantposts, only: [:create, :destroy]
resources :plants
到
resources :plants do
resources :plantposts, only: [:create, :destroy]
end
答案 1 :(得分:0)
这结果是一个语法问题。工作创建代码是:
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
我确信有一种更清洁的方法可以做到,但我还在继续。