创建方法失败

时间:2018-11-09 22:38:33

标签: ruby-on-rails

我只是从Rails开始,但存在以下问题: 我的文章模型如下所示:

#<Article id: nil, title: nil, body: nil, created_at: nil, updated_at: nil, image_file_name: nil, image_content_type: nil, image_file_size: nil, image_updated_at: nil, author_id: nil, month_id: nil, month_name: nil>

而我在articles_controller中的创建方法是这样(使用author_id,month_id和month_name的初步测试值):

def create
 @article = Article.new(article_params)
 @article.author_id = 4
 @article.month_id = 6
 @article.month_name = 'June'
 @article.save
 redirect_to article_path(@article)
end

带有helper_function

def article_params
 params.require(:article).permit(:title, :body, :tag_list, :image)
end

很遗憾,创建新文章无效。

通过运行带有三个迁移文件的db:migrate,我在现有模型中添加了author_id,month_id和month_name:

1)* _add_author_to_articles.rb

class AddAuthorToArticles < ActiveRecord::Migration[5.2]
  def change
    add_reference :articles, :author, foreign_key: true
  end
end

2)* _create_months.rb

class CreateMonths < ActiveRecord::Migration[5.2]
  def change
    create_table :months do |t|
      t.string :month_name

      t.timestamps
    end
  end
end

3)* _add_month_to_articles.rb

class AddMonthToArticles < ActiveRecord::Migration[5.2]
  def change
    add_reference :articles, :month, foreign_key: true
    add_column :articles, :month_name, :string
  end
end

然后我在article.rb中添加了

belongs_to :author
belongs_to :month

和month.rb

has_many :articles

和author.rb

has_many :articles

当我尝试在网络浏览器中创建文章时,出现以下消息

No route matches {:action=>"show", :controller=>"articles", :id=>nil}, missing required keys: [:id]

redirect_to article_path(@article)

被突出显示。 在服务器窗口中,服务器日志为

Started POST "/articles" for 127.0.0.1 at 2018-11-10 07:10:55 +0100 (0.2ms) SELECT "schema_migrations"."version" FROM "schema_migrations" ORDER BY "schema_migrations"."version" ASC ↳ /Users/inga/.rvm/gems/ruby-2.5.3/gems/activerecord-5.2.1/lib/active_record/log_subscriber.rb:98 Processing by ArticlesController#create as HTML Parameters: {"utf8"=>"✓", "authenticity_token"=>"srDMtFNEQmvRoiPEMMasuiUZ88iwSnYYdXaYRWLE00SXQM1aftxVkk7MS6mj/9USH0dDKRMPyzu0tu8spO+i5g==", "article"=>{"title"=>"Hello there!", "body"=>"Fun", "tag_list"=>""}, "commit"=>"Create Article"} Author Load (0.2ms) SELECT "authors".* FROM "authors" WHERE "authors"."id" = ? LIMIT ? [["id", 4], ["LIMIT", 1]] ↳ /Users/inga/.rvm/gems/ruby-2.5.3/bundler/gems/sorcery-e3045a94bbb0/lib/sorcery/adapters/active_record_adapter.rb:86 (0.1ms) begin transaction ↳ app/controllers/articles_controller.rb:29 Author Load (0.2ms) SELECT "authors".* FROM "authors" WHERE "authors"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]] ↳ app/controllers/articles_controller.rb:29 Month Load (0.1ms) SELECT "months".* FROM "months" WHERE "months"."id" = ? LIMIT ? [["id", 6], ["LIMIT", 1]] ↳ app/controllers/articles_controller.rb:29 (0.1ms) rollback transaction ↳ app/controllers/articles_controller.rb:29 Completed 500 Internal Server Error in 65ms (ActiveRecord: 2.7ms)

我在文章模型中添加了author_id,month_id和month_name之后,出现了问题。在一切正常之前。

有人可以告诉我在模型中添加新参数时我做错了什么或忘记了什么吗? 也许我不能手动将author_id和month_id设置为4和6?最终,它们应该是当前用户ID和创建文章的月份(1到12)。但是我想过,在找出语法之前,我会先尝试使用一些人为设置的值。 感谢您的帮助!

0 个答案:

没有答案