извините за этот вопрос, но я борюсь с этой проблемой часами и не могу найти ответ нигде.Модель модели Rails f.select не присваивает выбранное значение клавише модели
Вот вещь, у меня есть приложение рельсы с «Reservation» и модели «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>
Что выбрать его supose присвоить значение ключа «space_id» в резервации, который создается (таблица столбца создается). Но когда я проверяю последнее резервирование в консоли Rails, значение space_id равно «nil». Что я делаю не так?
Большое спасибо за вашу помощь
резервирование файла контроллера:
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
Space файл контроллера:
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 %>
Просьба опубликовать всю форму и соответствующий код контроллера. – dp7
Это то, что вы просили? @dkp – Jmorazano