新的rails用户在这里。我正在尝试让我的日程安排形式存储一系列“日子”,但经过多次尝试后我无法使其工作。
以下是我的代码
*的时间表/ _form.html.erb:*
<%= simple_form_for @schedule do |f| %>
<% if @schedule.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@schedule.errors.count, "error") %> prohibited this schedule from being saved:</h2>
<ul>
<% @schedule.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<%= f.input :section_id do %>
<%= f.select :section_id, Section.all.map{|s| [s.seccon, s.id]}, :include_blank => true %>
<% end %>
<%= f.association :subject %>
<%= f.collection_select :day_ids, @days, :id, :name, {}, {:multiple => true, :size => 1} %>
<div class="field">
<%= f.label :start_time %>
<%= f.time_select :start_time %>
</div>
<%= f.input :professor do %>
<%= f.select :professor_id, Professor.all.map{|j| [j.procon, j.id]}, :include_blank => true %>
<% end %>
<%= f.association :room %>
<%= f.button :submit %>
<% end %>
*的 schedules_controller.rb:*
class SchedulesController < ApplicationController
before_action :set_schedule, only: [:show, :edit, :update, :destroy]
# GET /schedules
# GET /schedules.json
def index
@schedules = Schedule.all
@days = Day.all
end
# GET /schedules/1
# GET /schedules/1.json
def show
end
# GET /schedules/new
def new
@schedule = Schedule.new
@days = Day.all
end
# GET /schedules/1/edit
def edit
end
# POST /schedules
# POST /schedules.json
def create
@schedule = Schedule.new(schedule_params)
@days = Day.all
respond_to do |format|
if @schedule.save
format.html { redirect_to @schedule, notice: 'Schedule was successfully created.' }
format.json { render action: 'show', status: :created, location: @schedule }
else
format.html { render action: 'new' }
format.json { render json: @schedule.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /schedules/1
# PATCH/PUT /schedules/1.json
def update
respond_to do |format|
if @schedule.update(schedule_params)
format.html { redirect_to @schedule, notice: 'Schedule was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: @schedule.errors, status: :unprocessable_entity }
end
end
end
# DELETE /schedules/1
# DELETE /schedules/1.json
def destroy
@schedule.destroy
respond_to do |format|
format.html { redirect_to schedules_url }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_schedule
@schedule = Schedule.find(params[:id])
@days = Day.all
end
# Never trust parameters from the scary internet, only allow the white list through.
def schedule_params
params.require(:schedule).permit(:section_id, :subject_id, :start_time, :finish_time_id, :professor_id, :room_id, :day_ids)
end
end
的 schedule.rb: 的
class Schedule < ActiveRecord::Base
belongs_to :section
belongs_to :subject
belongs_to :finish_time
has_and_belongs_to_many :days
accepts_nested_attributes_for :days, :allow_destroy => true
validates :section_id, :subject_id, :start_time, :professor_id, :room_id, :presence => true
belongs_to :professor
belongs_to :room
end
的 day.rb: 的
class Day < ActiveRecord::Base
has_and_belongs_to_many :schedules
default_scope { order(:id)}
has_paper_trail
validates :name, :desc, :presence => true
end
答案 0 :(得分:1)
正如所说here,最好的办法是在Schedule和Day之间创建一个has_many模型关系。您需要一个单独的连接表来使关系工作。它将:schedule_id和day_id作为两列。你这样做是因为你有很多&gt;很多关系。可能有许多计划属于一天和多天属于计划。
我在我的应用中使用了这种情况:
<强> Recipe.rb 强>
class Recipe < ActiveRecord::Base
has_and_belongs_to_many :wines
default_scope { order(:name) }
end
<强> Wine.rb 强>
class Wine < ActiveRecord::Base
has_and_belongs_to_many :recipes
accepts_nested_attributes_for :recipes, :allow_destroy => true
end
<强>移植强>
class AddRecipesWinesJoinTable < ActiveRecord::Migration
def self.up
create_table :recipes_wines, :id => false do |t|
t.column :recipe_id, :integer, :null => false
t.column :wine_id, :integer, :null => false
end
add_index :recipes_wines, [:wine_id]
end
def self.down
remove_index :recipes_wines, [:wine_id]
drop_table :recipes_wines
end
end
<强> _wine_form.html.erb 强>
# @recipes is Recipe.all generated by the controller
<%= w.collection_select :recipe_ids, @recipes, :id, :name, {}, {:multiple => true, :size => 6, :style => 'width:100%'} %>
希望这有帮助。