0

新的 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

时间表.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

日.rb:

class Day < ActiveRecord::Base

has_and_belongs_to_many :schedules
default_scope { order(:id)}

has_paper_trail

validates :name, :desc, :presence => true

end
4

1 回答 1

1

正如这里所说,最好的办法是在 Schedule 和 Day 之间创建一个 has_many 模型关系。您将需要一个单独的连接表来使关系正常工作。它将具有: schedule_id 和 day_id 作为两列。你会这样做是因为你有很多>很多的关系。可以有多个时间表属于一天,也可以有很多天属于一个时间表。

我在我的应用程序中使用了这个场景:

食谱.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%'} %>

希望这可以帮助。

于 2013-10-16T22:35:00.667 回答