我正在使用模型开发一个带有模型的项目
class Party < ApplicationRecord
has_many :bills
end
class Bill < ApplicationRecord
belongs_to :party
has_many :details
end
class Detail< ApplicationRecord
belongs_to :bill
end
我的控制器类
class PartiesController < ApplicationController
before_action :set_party, only: [:show, :edit, :update, :destroy]
def index
@parties = Party.all
end
def show
@parties = Party.find(params[:id])
end
def new
@party = Party.new
@bill = Bill.new
end
def edit
@parties = Party.find(params[:id])
end
def create
@party = Party.new(party_params)
@bill = Bill.new
respond_to do |format|
if @party.save
format.html { redirect_to @party, notice: 'Party was successfully created.' }
format.json { render :show, status: :created, location: @party }
else
format.html { render :new }
format.json { render json: @party.errors, status: :unprocessable_entity }
end
end
end
def update
respond_to do |format|
if @party.update(party_params)
format.html { redirect_to @party, notice: 'Party was successfully updated.' }
format.json { render :show, status: :ok, location: @party }
else
format.html { render :edit }
format.json { render json: @party.errors, status: :unprocessable_entity }
end
end
end
def destroy
@party.destroy
respond_to do |format|
format.html { redirect_to parties_url, notice: 'Party was successfully destroyed.' }
format.json { head :no_content }
end
end
private
def set_party
@party = Party.find(params[:id])
end
def party_params
params.require(:party).permit(:name, :address)
end
end
现在我想存储Detail类的参数,并在视图中显示它。
在这种情况下如何定义路线?我现有的路线是:
Rails.application.routes.draw do
root 'parties#index'
resources :parties do
resources :bills
end
end
对于课程详情,我该如何定义路线?
提前致谢
答案 0 :(得分:0)
要存储Detail
参数,您必须使用DetailsController
。使用PartiesController
不会是“Rails Way”e.q。好办法。
您可以像这样定义您的路线:
的routes.rb
Rails.application.routes.draw do
root 'parties#index'
resources :parties do
resources :bills do
resources :details
end
end
end
details
生成导轨支架,它将满足您当前的需求。这些观点将是“独立的”等等。