我是Rails的新手,正在研究一个示例应用。该应用程序背后的想法是,它是一个计算机化的登记表,供孩子们乘坐校车。有四种型号:Family,Kid,SchoolRide和HomeRide。对于Family和Kid,我生成了完整的脚手架,但是对于SchoolRide和HomeRide,它们只是具有布尔字段的模型,每个模型是孩子在早上登记到校车还是在下午回家时检查。
我希望能够让用户从孩子秀视图中呈现的表单中检查孩子,但是我无法从孩子控制器创建我的骑行模型的实例。如何设置视图,路由和控制器?在哪里/如何将参数传递给儿童控制器中的骑行模型?
这是我的表格呈现在孩子的节目视图中。目前,我收到语法错误。
<%= form_with(model: @school_ride, remote: true), :url => school_rides_path, :html => { :method => :post } do |form| %>
<div><p>
<%= form.label :check_in %><br>
<%= form.check_box :check_in %><br>
</p>
</div>
<div>
<%= form.hidden_field :kid_id, value: @kid.id %>
</div>
<p>
<%= form.submit %>
</p>
<% end %>
这是我的模特:
class Kid < ApplicationRecord
belongs_to :family
has_many :school_rides
has_many :home_rides
end
class HomeRide < ApplicationRecord
belongs_to :kid
end
class SchoolRide < ApplicationRecord
belongs_to :kid
end
以下是我的孩子控制器的一些相关部分:
def show
@family = Family.all
@school_ride = SchoolRide.new
end
# GET /kids/new
def new
@kid = Kid.new
end
# GET /kids/1/edit
def edit
end
# POST /kids
# POST /kids.json
def create
@kid = Kid.new(kid_params)
respond_to do |format|
if @kid.save
format.html { redirect_to family_path(id: @kid.family_id), notice: 'Kid was successfully created.' }
format.json { render :show, status: :created, location: @kid }
else
format.html { render :new }
format.json { render json: @kid.errors, status: :unprocessable_entity }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_kid
@kid = Kid.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def kid_params
params.require(:kid).permit(:name, :birthdate, :grade, :family_id)
end
def school_ride_params
params.require(:school_ride).permit(:check_in)
end
以下是我的部分路线:
resources :kids
resources :school_rides, only: [:new, :create]
答案 0 :(得分:0)
试试这个:
<%=form_for @school_rid, remote: true do |form| %>