在文章视图(_form)中添加复选框后,我收到以下错误
ArticlesController中的NoMethodError #create nil的未定义方法`name':NilClass
摘录来源(第52行):50 51 52 53 54 55
@article = Article.new(article_params)
@article.user = current_user
if @article.save
flash[:success]="Your Tip has been Created"
redirect_to article_path(@article)
else
这是文章视图中的视图页面_form:
<%=render'shared/errors',obj: @article%>
<div class='row'>
<div class='col-xs-12'>
<%= form_for(@article, :html => {class: "form-horizontal", role: "form"}) do |f| %>
<div class='form-group'>
<div class="control-label col-sm-2">
<%= f.label :title %>
</div>
<div class ="col-sm-7">
<%= f.text_field :title,class:"form-control",placeholder:"Title of Tip",autofocus:true %>
</div>
</div>
<div class='form-group'>
<div class="control-label col-sm-2">
<%= f.label :description %>
</div>
<div class ="col-sm-8">
<%= f.text_area :description,rows:10,class:"form-control",placeholder:"Make it short" %>
</div>
</div>
<div class="form-group">
<div class="row">
<div class="col-sm-offset-2 col-sm-10">
<%= f.collection_check_boxes :category_ids, Category.all, :id, :name do |cb| %>
<% cb.label(class: "checkbox-inline input_checkbox") {cb.check_box(class: "checkbox") + cb.text} %>
<% end %>
</div>
</div>
</div>
<div class='form-group'>
<div class="col-sm-offset-2 col-sm-10">
<%= f.submit class:'btn btn-danger btn-lg'%>
</div>
</div>
<div class="col-xs-4 col-xs-offset-4">
[<%= link_to 'Cancel and Back to Tips',articles_path%>]
</div>
<% end %>
</div>
</div>
这是文章控制器:
class ArticlesController < ApplicationController
before_action :set_article,only: [:edit ,:update ,:destroy,:show]
before_action :require_user,except: [:index,:show]
before_action :require_same_user,only:[:edit,:update,:destroy]
def home
redirect_to articles_path
end
def index
@articles = Article.all.order("created_at DESC").paginate(page:params[:page], per_page: 6 )
end
def new
@article = Article.new
end
def destroy
@article.destroy
redirect_to articles_path
flash[:danger] = "ِYour Tip has been Deleted"
end
def edit
end
def update
if @article.update(article_params)
flash[:success] = "ِYour Tip has been Updated"
redirect_to article_path(@article)
else
render 'edit'
end
end
def show
end
def create
@article = Article.new(article_params)
@article.user = current_user
if @article.save
flash[:success]="Your Tip has been Created"
redirect_to article_path(@article)
else
render 'new'
end
end
private
def set_article
@article = Article.find(params[:id])
end
def article_params
params.require(:article).permit(:title, :description, category_ids:[])
end
def require_same_user
if current_user != @article.user and !current_user.admin?
flash[:danger]="You can only Edit your own Tips"
redirect_to articles_path
end
end
end
我添加了复选框以允许用户为他的文章选择类别,但收到了提及错误。
这是存储库链接: https://github.com/essam11/Tips-Gator`
enter code here
更新: 这是我在cmd中看到的错误
C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/activesupport-4.0.0/lib/active_support/values/time_zone.rb:282: warning: circular argument reference - now
=> Booting WEBrick
=> Rails 4.0.0 application starting in development on http://0.0.0.0:3000
=> Run `rails server -h` for more startup options
=> Ctrl-C to shutdown server
[2017-01-25 14:26:27] INFO WEBrick 1.3.1
[2017-01-25 14:26:27] INFO ruby 2.2.4 (2015-12-16) [x64-mingw32]
[2017-01-25 14:26:27] INFO WEBrick::HTTPServer#start: pid=3884 port=3000
Started POST "/articles" for 127.0.0.1 at 2017-01-25 14:26:28 -0800
ActiveRecord::SchemaMigration Load (1.0ms) SELECT "schema_migrations".* FROM "schema_migrations"
Processing by ArticlesController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"PhHhY2JjMjNVM+bT8UmXTRcduOdq9DRKJxKzJz3OfIQ=", "article"=>{"title"=>"xxxxxxxxxxxxxxx", "description"=>"xxxxxxxxxxxxxxxx", "category_ids"=>["1", ""]}, "commit"=>"Create Article"}
User Load (7.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 9]]
C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/activerecord-4.0.0/lib/active_record/associations/has_many_association.rb:75: warning: circular argument reference - reflection
C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/activerecord-4.0.0/lib/active_record/associations/has_many_association.rb:79: warning: circular argument reference - reflection
C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/activerecord-4.0.0/lib/active_record/associations/has_many_association.rb:83: warning: circular argument reference - reflection
C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/activerecord-4.0.0/lib/active_record/associations/has_many_association.rb:102: warning: circular argument reference - reflection
Category Load (0.0ms) SELECT "categories".* FROM "categories" WHERE "categories"."id" = ? LIMIT 1 [["id", 1]]
(0.0ms) begin transaction
Category Exists (1.0ms) SELECT 1 AS one FROM "categories" WHERE ("categories"."name" = 'Windows' AND "categories"."id" != 1) LIMIT 1
SQL (42.0ms) INSERT INTO "articles" ("created_at", "description", "title", "updated_at", "user_id") VALUES (?, ?, ?, ?, ?) [["created_at", Wed, 25 Jan 2017 22:26:29 UTC +00:00], ["description", "xxxxxxxxxxxxxxxx"], ["title", "xxxxxxxxxxxxxxx"], ["updated_at", Wed, 25 Jan 2017 22:26:29 UTC +00:00], ["user_id", 9]]
SQL (1.0ms) INSERT INTO "article_categories" ("article_id", "category_id") VALUES (?, ?) [["article_id", 66], ["category_id", 1]]
(57.0ms) rollback transaction
Completed 500 Internal Server Error in 470ms
NoMethodError (undefined method `name' for nil:NilClass):
app/controllers/articles_controller.rb:52:in `create'
Rendered C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/actionpack-4.0.0/lib/action_dispatch/middleware/templates/rescues/_source.erb (2.0ms)
Rendered C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/actionpack-4.0.0/lib/action_dispatch/middleware/templates/rescues/_trace.erb (13.0ms)
Rendered C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/actionpack-4.0.0/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (2.0ms)
Rendered C:/Ruby22-x64/lib/ruby/gems/2.2.0/gems/actionpack-4.0.0/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (1231.8ms)