Rails 5联接表创建一个将新行添加到联接表的表单

时间:2018-10-08 20:43:44

标签: ruby-on-rails ruby-on-rails-5 jointable

您好,我正在为工作构建一个基本的报价应用程序。

我有一个提案,一个部分和一个联接表Proposal_Sections

模型

class Proposal < ApplicationRecord
  has_many :proposal_sections
  has_many :sections, through: :proposal_sections
end

class ProposalSection < ActiveRecord::Base
  belongs_to :proposal
  belongs_to :section
end

class Section < ApplicationRecord
  has_many :proposal_sections
  has_many :proposals, through: :proposal_sections
end

我希望用户能够在显示页面上将现有部分添加到投标中,因此我创建了以下表单。

投标/展示表格

<%= form_for @proposal do |f| %>
    <%= f.fields_for :proposal_sections do |t| %>
      <%= t.collection_select :section_id, Section.all, :id, :title %>
      <%= t.submit 'Add Section' %>
    <% end %>
  <% end %>

该表单显示在页面上,不会产生任何错误,但结果将无法保存。

我在控制台中找到了

Started GET "/proposals/1" for 127.0.0.1 at 2018-10-09 06:33:15 +1000
Processing by ProposalsController#show as HTML
  Parameters: {"id"=>"1"}
  Proposal Load (0.4ms)  SELECT  "proposals".* FROM "proposals" WHERE "proposals"."id" = $1 LIMIT $2  [["id", 1], ["LIMIT", 1]]
  ↳ app/controllers/proposals_controller.rb:75
  User Load (0.4ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2  [["id", 1], ["LIMIT", 1]]
  ↳ /Users/jeremybray/.rvm/rubies/ruby-2.4.2/lib/ruby/gems/2.4.0/gems/puma-3.12.0/lib/puma/configuration.rb:225
  Rendering proposals/show.html.erb within layouts/application
  User Load (1.1ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2  [["id", 1], ["LIMIT", 1]]
  ↳ app/views/proposals/show.html.erb:17
   (0.6ms)  SELECT COUNT(*) FROM "sections" INNER JOIN "proposal_sections" ON "sections"."id" = "proposal_sections"."section_id" WHERE "proposal_sections"."proposal_id" = $1  [["proposal_id", 1]]
  ↳ app/views/proposals/show.html.erb:23
  Section Load (0.4ms)  SELECT "sections".* FROM "sections"
  ↳ app/views/proposals/show.html.erb:30
  Rendered proposals/show.html.erb within layouts/application (9.7ms)
  Rendered shared/_head.html.erb (90.8ms)
  Announcement Load (0.4ms)  SELECT  "announcements".* FROM "announcements" ORDER BY "announcements"."published_at" DESC LIMIT $1  [["LIMIT", 1]]
  ↳ app/helpers/announcements_helper.rb:3
  Rendered shared/_navbar.html.erb (2.5ms)
  Rendered shared/_notices.html.erb (0.3ms)
  Rendered shared/_footer.html.erb (0.4ms)
Completed 200 OK in 134ms (Views: 123.9ms | ActiveRecord: 3.2ms)

控制器

class ProposalsController < ApplicationController
  before_action :set_proposal, only: [:show, :edit, :update, :destroy]
  before_action :authenticate_user!
  # GET /proposals
  # GET /proposals.json
  def index
    @proposals = current_user.proposals.all
  end

  # GET /proposals/1
  # GET /proposals/1.json
  def show
  end

  def add
    @proposal = Proposal.find(params[:proposal_id])
    if request.post?
      @section = Section.find(params[:section_id])
      @proposal.proposal_sections << @section
    end
  end

  # GET /proposals/new
  def new
    @proposal = Proposal.new
  end

  # GET /proposals/1/edit
  def edit
  end

  # POST /proposals
  # POST /proposals.json
  def create
    @proposal = current_user.proposals.new(proposal_params)

    respond_to do |format|
      if @proposal.save
        format.html { redirect_to @proposal, notice: 'Proposal was successfully created.' }
        format.json { render :show, status: :created, location: @proposal }
      else
        format.html { render :new }
        format.json { render json: @proposal.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /proposals/1
  # PATCH/PUT /proposals/1.json
  def update
    respond_to do |format|
      if @proposal.update(proposal_params)
        format.html { redirect_to @proposal, notice: 'Proposal was successfully updated.' }
        format.json { render :show, status: :ok, location: @proposal }
      else
        format.html { render :edit }
        format.json { render json: @proposal.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /proposals/1
  # DELETE /proposals/1.json
  def destroy
    @proposal.destroy
    respond_to do |format|
      format.html { redirect_to proposals_url, notice: 'Proposal was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_proposal
      @proposal = Proposal.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def proposal_params
      params.require(:proposal).permit(:project, :client, :user, proposal_sections: [], section_ids: [])
    end
end

我的路线

require 'sidekiq/web'

Rails.application.routes.draw do
  resources :sections
  resources :proposals do
    match :add, via: [:get, :post]
  end
  namespace :admin do
      resources :users
      resources :announcements
      resources :notifications
      resources :services

      root to: "users#index"
    end
  get '/privacy', to: 'home#privacy'
  get '/terms', to: 'home#terms'
  resources :notifications, only: [:index]
  resources :announcements, only: [:index]
  authenticate :user, lambda { |u| u.admin? } do
    mount Sidekiq::Web => '/sidekiq'
  end

  devise_for :users, controllers: { omniauth_callbacks: "users/omniauth_callbacks" }
  root to: 'home#index'
  # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end

我确定我错过了一些简单的事情,感谢您的耐心配合和帮助

0 个答案:

没有答案