簡體   English   中英

ActiveRecord :: StatementInvalid,SQLite3 :: SQLException:無此類列:

[英]ActiveRecord::StatementInvalid, SQLite3::SQLException: no such column:

我正在研究我的RoR技能,並且為了進行學校作業,我被要求在發布政策中創建作用域子類,以允許具有特定角色(管理員,主持人,成員和來賓)的用戶能夠通過各種訪問量。 當以管理員或主持人身份登錄時,所有帖子均應可見。 作為成員,只有登錄成員發布的帖子才可見。 作為來賓,沒有帖子應該可見。

當我以管理員或主持人的身份登錄時,我可以查看該網站上的所有帖子,但是,當以會員或訪客的身份查看該網站時,我無法正常工作並收到錯誤消息。 有人可以幫忙嗎?

我收到的錯誤:

ActiveRecord::StatementInvalid in Posts#index

Showing /Users/Jason/code/bloccit/app/views/posts/index.html.erb where line #7 raised:

SQLite3::SQLException: no such column: posts.published: SELECT "posts".* FROM "posts"  WHERE "posts"."published" = 't'  ORDER BY created_at DESC

<%= link_to "New Post", new_post_path, class: 'btn btn-success' %>
  <% end %>

  <% @posts.each do |post| %>
    <div class="media">
      <div class="media-body">
        <h4 class="media-heading">

這是我的views / posts / index.html.erb

<h1>All Posts</h1>

<% if policy(Post.new).create? %>
  <%= link_to "New Post", new_post_path, class: 'btn btn-success' %>
<% end %>

<% @posts.each do |post| %>
  <div class="media">
    <div class="media-body">
      <h4 class="media-heading">
        <%= link_to post.title, post %>
      </h4>
      <small>
        submitted <%= time_ago_in_words(post.created_at) %> ago by <%= post.user.name %><br>
        <%= post.comments.count %> Comments
      </small>
    </div>
  </div>
<% end %>

post_policy.rb

class PostPolicy < ApplicationPolicy
  def index?
    true
  end

  class Scope < Scope
    attr_reader :user, :scope

    def initialize(user, scope)
      @user = user
      @scope = scope
    end

    def resolve
      if user.admin? || user.moderator?
        scope.all
      else
        scope.where(:published => true)
      end
    end
  end
end

application_policy.rb

class ApplicationPolicy
  attr_reader :user, :record

  def initialize(user, record)
    @user = user
    @record = record
  end

  def index?
    false
  end

  def show?
    scope.where(id: record.id).exists?
  end

  def create?
    user.present?
  end

  def new?
    create?
  end

  def update?
    user.present? && (record.user == user || user.admin?)
  end

  def edit?
    update?
  end

  def destroy?
    update?
  end

  def scope
    record.class
  end

  class Scope
    attr_reader :user, :scope

    def initialize(user, scope)
      @user = user
      @scope = scope
    end

    def resolve
      scope
    end
  end
end

post.rb

class Post < ActiveRecord::Base
  has_many :comments
  belongs_to :user

  default_scope { order('created_at DESC') }
end

user.rb

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable, :confirmable

  has_many :posts

  def admin?
    role == 'admin'
  end

  def moderator?
    role == 'moderator'
  end

  def member?
    role == 'member'
  end
end

最后,這是我的posts_controller.rb

class PostsController < ApplicationController
  def index
    @posts = policy_scope(Post)
  end

  def show
    @post = Post.find(params[:id])
  end

  def new
    @post = Post.new
    authorize @post
  end

  def create
    @post = Post.new(params.require(:post).permit(:title, :body))
    @post.user = current_user
    authorize @post
    if @post.save
      flash[:notice] = "Post was saved."
      redirect_to @post
    else
      flash[:error] = "There was an error saving the post.  Please try again."
      render :new
    end
  end

  def edit
    @post = Post.find(params[:id])
    authorize @post
  end

  def update
    @post = Post.find(params[:id])
    authorize @post
    if @post.update_attributes(params.require(:post).permit(:title, :body))
      flash[:notice] = "Post was updated."
      redirect_to @post
    else
      flash[:error] = "There was an error saving the post.  Please try again."
      render :edit
    end
  end
end

您的PostPolicy有此方法

def resolve
  if user.admin? || user.moderator?
     scope.all
   else
     scope.where(:published => true)
   end
end

就像您說的那樣,以成員身份登錄時會出錯,因此在上述方法中, else塊將被執行,並尋找:publishedtrueposts 因此,由於您在posts表中沒有已 published列,因此會觸發錯誤。

要解決此問題,請創建一個遷移文件,以將publishedboolean屬性(默認值設置為trueposts表。

您的遷移文件將如下所示

class AddPublishedToPosts < ActiveRecord::Migration
  def change
    add_column :posts, :published, :boolean, :default => true
  end
end

並做rake:db:migrate

SQLite3::SQLException: no such column: posts.published:

錯誤說您沒有在表中published屬性,根據查詢,該屬性當然是boolean類型

SELECT "posts".* FROM "posts"  WHERE "posts"."published" = 't'  ORDER BY created_at DESC

請向您的Post模型添加遷移:

$ rails g migration add_published_to_posts published:boolean

然后將默認值添加到您published屬性

class AddPublishedToPosts < ActiveRecord::Migration
  def change
    add_column :posts, :published, :boolean, :default => false
  end
end

最后,運行: $ rake db:migrate


[英]#<ActiveRecord::StatementInvalid: SQLite3::SQLException: no such column: users.sender_id:

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM