Cancan和cancancan宝石都表现得很奇怪

时间:2015-11-19 00:59:54

标签: ruby-on-rails ruby-on-rails-4 devise cancan cancancan

我正在玩康康宝石。我试图在rails 4.2.1项目中实现它。我的红宝石版本2.2.3。这些是我刚刚安装的两个宝石

gem 'devise', '~> 3.5', '>= 3.5.2'
gem 'cancan', '~> 1.6', '>= 1.6.10'

我根据他们的文档配置了cancan gem和devise。以下是我的代码

ability.rb

class Ability
  include CanCan::Ability

  def initialize(user)
     can :manage, :all
 end
end

posts_controller.rb

class PostsController < ApplicationController
  before_action :authenticate_user!
  load_and_authorize_resource
  before_action :set_post, only: [:show, :edit, :update, :destroy]

def create
    a = 10
    @post = Post.new(post_params)
    @post.user_id = current_user.id

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

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

    # Never trust parameters from the scary internet, only allow the white list through.
    def post_params
      params.require(:post).permit(:title, :content, :user_id, :comments_enabled, :views)
    end
end

发布表格

<%= form_for(@post) do |f| %>
          <% if @post.errors.any? %>
              <div id="error_explanation">
                <h2><%= pluralize(@post.errors.count, "error") %> prohibited this post from being saved:</h2>

                <ul>
                  <% @post.errors.full_messages.each do |message| %>
                      <li><%= message %></li>
                  <% end %>
                </ul>
              </div>
          <% end %>             
          <div class="panel panel-sea margin-bottom-40">
            <div class="panel-heading">
              <h3 class="panel-title"><i class="fa fa-tasks"></i> Horizontal Form</h3>
            </div>
            <div class="panel-body">

                <div class="form-group">
                  <%#= f.label :title, class: "col-lg-2 control-label" %>
                  <div class="col-lg-12">
                    <%= f.text_field :title, class: "form-control", placeholder: "Title" %>
                  </div>
                </div>
                <div class="form-group">
                  <%#= f.label :content, class: "col-lg-2 control-label" %>
                  <div class="col-lg-12">
                    <%= f.text_area :content, class: "form-control", placeholder: "Content" %>
                  </div>
                </div>
                <div class="form-group">
                  <!--<div class="col-lg-offset-2 col-lg-10">-->
                  <div class="col-lg-12">
                    <div class="actions">
                      <%= f.check_box :comments_enabled %>
                      <%= f.label "Enable Comments" %>
                    </div>
                  </div>
                </div>
                <div class="form-group">
                  <div class="col-lg-12">
                    <div class="actions">
                      <%= f.submit "Post Your Article", class: "btn-u btn-u-sea" %>
                    </div>
                  </div>
                </div>

            </div>
          </div>              
      <% end %>

现在,如果我提交表单,我会收到此错误

NoMethodError in PostsController#create
undefined method `+' for nil:NilClass

并且有些东西一直在服务器上运行。如果我执行第二个请求,那么页面会继续加载,但该请求永远不会结束。我想有些无限循环在这里工作。

在此之后我尝试卸载cancan gem并安装了cancancan #gem 'cancancan', '~> 1.10',现在如果我提交帖子表单,我会收到此错误

ActiveModel::ForbiddenAttributesError in PostsController#create 

 Extracted source (around line #21):   
      def sanitize_for_mass_assignment(attributes)
        if attributes.respond_to?(:permitted?) && !attributes.permitted?
          raise ActiveModel::ForbiddenAttributesError
        else
          attributes
        end

在帖子控制器中,如果我注释掉这一行

#load_and_authorize_resource

然后一切都很完美。我不明白我在这里做错了什么。我该如何解决这个问题?

0 个答案:

没有答案