通过具有多个ID的协会工作(Rails 4.2.3)

时间:2016-10-12 18:53:39

标签: ruby-on-rails ruby ruby-on-rails-4

我正在完成我的第一个Rails项目。我正在创建一个用户登录并创建一个板的应用程序,该板特定于用户的ID。在该板内,用户可以创建列表。每个列表应该与该板的ID绑定。我遇到的问题是创建一个列表。它没有保存。我看到的具体错误是Could not find the association :memberships in model List我可以看到列表被映射到Board的ID存在问题。当我查看Rails服务器的输出时,我看到显示一块板(应该显示该板的所有列表)没有用户ID或板ID。

显示用户的主板

Started GET "/boards" for ::1 at 2016-10-12 14:26:33 -0400
Processing by BoardsController#index as HTML
  User Load (0.1ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1  [["id", 1]]
  Board Load (0.1ms)  SELECT "boards".* FROM "boards" WHERE "boards"."user_id" = ?  [["user_id", 1]]
  CACHE (0.0ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1  [["id", 1]]
  CACHE (0.0ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1  [["id", 1]]
  CACHE (0.0ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1  [["id", 1]]
  Rendered boards/index.html.erb within layouts/application (3.4ms)
  Rendered layouts/_flash.html.erb (0.1ms)
Completed 200 OK in 168ms (Views: 166.1ms | ActiveRecord: 0.2ms)

VS显示该用户的列表

Started GET "/lists.3" for ::1 at 2016-10-12 14:28:26 -0400
Processing by ListsController#index as
  User Load (0.1ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1  [["id", 1]]
  List Load (0.3ms)  SELECT "lists".* FROM "lists"
  Rendered lists/index.html.erb within layouts/application (2.3ms)
  Rendered layouts/_flash.html.erb (0.1ms)
Completed 200 OK in 181ms (Views: 169.2ms | ActiveRecord: 0.9ms)

相关代码......

模型

board.rb

    class Board < ActiveRecord::Base
  belongs_to :owner, class_name: "User", foreign_key: "user_id"

  has_many :lists
  has_many :memberships
  has_many :members, :through => :memberships, :source => :user
  has_many :lists, :through => :memberships, :source => :board

  validates_presence_of :title
  validates_presence_of :owner

  def is_private?
    return self.is_private
  end
end

list.rb

class List < ActiveRecord::Base
  belongs_to :board
  has_many :cards

  has_one :board, :through => :memberships, :source => :board

  validates_presence_of :title
  validates_presence_of :board
end

user.rb

class User < ActiveRecord::Base
  has_many :boards
  has_many :lists
  has_many :cards
  has_many :memberships
  has_many :board_memberships, :through => :memberships, :source => :board
  has_many :list_memberships, :through => :memberships, :source => :board

  has_secure_password

  validates :password,
    length: { :minimum => 8 },
    presence: true

  validates :username,
    uniqueness: true,
    presence: true

  validates_confirmation_of :password
end

membership.rb

class Membership < ActiveRecord::Base
  belongs_to :user
  belongs_to :board

  validates :user, presence: true, uniqueness: { scope: :board, message: "User is already a member of that board" }
  validates :board, presence: true
end

控制器

boards_controller.rb (我这里只添加了相关部分)

    class BoardsController < ApplicationController
      before_action :set_board, only: [:show, :edit, :update, :destroy]
      before_action :authenticate, except: [:show]
      before_action :authorize, except: [:show]
      before_action :authorize_owner, only: [:edit, :update, :destroy]
      before_action :authorize_membership, only: [:show]

      # GET /boards
      # GET /boards.json
      def index
        @boards = current_user.boards
      end

      # GET /boards/1
      # GET /boards/1.json
      def show
        @lists = current_user.board_id
      end

    private
        def set_board
          @board = Board.find(params[:id])
        end

        def board_params
          params.require(:board).permit(:title, :is_private)
        end

        def authorize_owner
          if current_user != @board.owner
            redirect_to root_url, :notice => 'Board not found or inaccessible' and return
          end
        end

        def authorize_membership
          return
          if @board.is_private?
            if !authenticate
              redirect_to root_url, :notice => 'Please login first' and return
            end

            if @board.owner != current_user || !@board.members.include?(current_user)
              redirect_to root_url, :notice => 'Board not found or inaccessible' and return
            end
          end
        end
  # POST /boards
  # POST /boards.json
  def create
    @board = Board.new(board_params)
    @board.owner = current_user

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

lists_controller.rb(整个文件)

class ListsController < ApplicationController
  before_action :set_list, only: [:show, :edit, :update, :destroy]
  before_action :authenticate, except: [:show]

  # GET /lists
  # GET /lists.json
  def index
    @lists = List.all
  end

  # GET /lists/1
  # GET /lists/1.json
  def show
    @lists = current_user.board_id
  end

  # GET /lists/new
  def new
    @list = List.new
  end

  # GET /lists/1/edit
  def edit
  end

  # POST /lists
  # POST /lists.json
  def create
    @list = List.new(list_params)

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

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

  # DELETE /lists/1
  # DELETE /lists/1.json
  def destroy
    @list.destroy
    respond_to do |format|
      format.html { redirect_to lists_url, notice: 'List was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

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

    # Never trust parameters from the scary internet, only allow the white list through.
    def list_params
      params.require(:list).permit(:title, :board_id)
    end
end

我已经阅读了几篇关于嵌套模型的SO帖子,以及Rails文档。我知道我的知识差距某处。我会很感激任何方向,因为我试图阅读其他人。问题和尝试的解决方案都失败了。

1 个答案:

答案 0 :(得分:1)

如果您希望List对象能够访问:memberships对象的Board,请执行...

class List << ActiveRecord::Base

  delegate :memberships, to: :board

删除 has_one :board, :through => :memberships, :source => :board因为那没有意义......你已经有了董事会关系而你没有会员关系可以用来建立不同的董事会关系。