对不起这个问题,但是我现在几个小时都在努力解决这个问题,无法在任何地方找到答案。
这是事情,我有一个轨道应用程序"预订"和" Space"具有以下关系的模型:
class Reservation < ActiveRecord::Base
belongs_to :space
belongs_to :user
end
class Space < ActiveRecord::Base
belongs_to :condo
has_many :reservations
end
当用户创建新的预订时,他可以从表格中选择下拉列表(f.select)中可用的空格。表单中的f.select看起来像这样:
<div class="field">
<%= @user_spaces = current_user.condo.spaces
f.select :space_id,
options_from_collection_for_select(@user_spaces, :id, :name), :prompt => "Select space"
%>
</div>
选择它来为键分配一个值&#34; space_id&#34;在正在创建的预留中(创建列表)。但是当我在Rails控制台中检查最后一个预留时,space_id值为&#34; nil&#34;。我做错了什么?
非常感谢您的帮助
保留控制器文件:
class ReservationsController < ApplicationController
before_action :set_reservation, only: [:show, :edit, :update, :destroy]
# GET /reservations
# GET /reservations.json
def index
@reservations = Reservation.all
end
# GET /reservations/1
# GET /reservations/1.json
def show
end
# GET /reservations/new
def new
@reservation = Reservation.new
end
# GET /reservations/1/edit
def edit
end
# POST /reservations
# POST /reservations.json
def create
@reservation = Reservation.new(reservation_params)
@user = current_user.id
@reservation.user_id = @user
respond_to do |format|
if @reservation.save
format.html { redirect_to @reservation, notice: 'Reservation was successfully created.' }
format.json { render :show, status: :created, location: @reservation }
else
format.html { render :new }
format.json { render json: @reservation.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /reservations/1
# PATCH/PUT /reservations/1.json
def update
respond_to do |format|
if @reservation.update(reservation_params)
format.html { redirect_to @reservation, notice: 'Reservation was successfully updated.' }
format.json { render :show, status: :ok, location: @reservation }
else
format.html { render :edit }
format.json { render json: @reservation.errors, status: :unprocessable_entity }
end
end
end
# DELETE /reservations/1
# DELETE /reservations/1.json
def destroy
@reservation.destroy
respond_to do |format|
format.html { redirect_to reservations_url, notice: 'Reservation was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_reservation
@reservation = Reservation.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def reservation_params
params.require(:reservation).permit(:eventdate)
end
end
空间控制器文件:
class SpacesController < ApplicationController
before_action :set_space, only: [:show, :edit, :update, :destroy]
# GET /spaces
# GET /spaces.json
def index
@spaces = Space.all
end
# GET /spaces/1
# GET /spaces/1.json
def show
end
# GET /spaces/new
def new
@space = Space.new
end
# GET /spaces/1/edit
def edit
end
# POST /spaces
# POST /spaces.json
def create
@space = Space.new(space_params)
respond_to do |format|
if @space.save
format.html { redirect_to @space, notice: 'Space was successfully created.' }
format.json { render :show, status: :created, location: @space }
else
format.html { render :new }
format.json { render json: @space.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /spaces/1
# PATCH/PUT /spaces/1.json
def update
respond_to do |format|
if @space.update(space_params)
format.html { redirect_to @space, notice: 'Space was successfully updated.' }
format.json { render :show, status: :ok, location: @space }
else
format.html { render :edit }
format.json { render json: @space.errors, status: :unprocessable_entity }
end
end
end
# DELETE /spaces/1
# DELETE /spaces/1.json
def destroy
@space.destroy
respond_to do |format|
format.html { redirect_to spaces_url, notice: 'Space was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_space
@space = Space.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def space_params
params.require(:space).permit(:name)
end
end
完整的预订表格:
<%= form_for(@reservation) do |f| %>
<% if @reservation.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@reservation.errors.count, "error") %> prohibited this reservation from being saved:</h2>
<ul>
<% @reservation.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :eventdate %><br>
<%= f.date_select :eventdate %>
</div>
<div class="field">
<%= @user = current_user.condo.spaces
f.select :space_id,
options_from_collection_for_select(@user, :id, :name), :prompt => "Select space"
%>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
答案 0 :(得分:1)
非常确定您需要在强力参数中允许space_id
属性。
def reservation_params
params.require(:reservation).permit(:eventdate, :space_id)
end
发生什么事情是当你去创建一个预订时,你传递了一组参数,这是reservation_params
的输出
@reservation = Reservation.new(reservation_params)
如果您的强参数中不允许space_id
,那么创建时它将为零。
如果这不是问题,你可以发布params到服务器的内容,以及reservation_params
的输出是什么。