【发布时间】:2016-09-04 12:25:30
【问题描述】:
我正在使用我在 youtube 上找到的教程使用 ruby on rails 创建一个论坛网站。到目前为止,我已经完成了 80%,没有任何问题。我已经重新观看了 10 多次视频,以确保没有语法错误或任何其他事故。基本上,人们在帖子上发布的 cmets 不会保存到数据库中,因此它们不会显示在我展示它们的 html 视图中。我知道他们没有保存,因为我在终端中检查了 cmets 的数据库,它以 0 计数返回。这是我在不同文件中的代码...
ROUTES.RB
Rails.application.routes.draw do
devise_for :users
resources :posts do
resources :comments
end
root 'posts#index'
end
create_cmets 的迁移文件
class CreateComments < ActiveRecord::Migration[5.0]
def change
create_table :comments do |t|
t.text :comment
t.references :post, foreign_key: true
t.references :user, foreign_key: true
t.timestamps
end
end
end
cmets_controller.rb
class CommentsController < ApplicationController
def create
@post = Post.find(params[:post_id])
@comment = @post.comments.create(params[:comment].permit(:comment))
if @comment.save
redirect_to post_path(@post)
else
render 'new'
end
end
end
_form.html.haml
= simple_form_for([@post, @post.comments.build]) do |f|
= f.input :comment
= f.submit
模型文件注释.rb
class Comment < ApplicationRecord
belongs_to :post
belongs_to :user
end
提交表单时记录
Started POST "/posts/2/comments" for ::1 at 2016-09-04 23:00:46 +1000
Processing by CommentsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"/Un6QNWL4BIUbjH5VYMhLRatTq2hokcKnZ3Jb4WzTlvhuZ5AN3gFkA5VHN2E6zsm0iDIx/sKarEfID7Nx4WwwQ==", "comment"=>{"comment"=>"1"}, "commit"=>"Create Comment", "post_id"=>"2"}
Post Load (0.3ms) SELECT "posts".* FROM "posts" WHERE "posts"."id" = ? LIMIT ? [["id", 2], ["LIMIT", 1]]
(0.1ms) begin transaction
(0.1ms) rollback transaction
Completed 500 Internal Server Error in 26ms (ActiveRecord: 0.5ms)
ActionView::MissingTemplate (Missing template comments/new, application/new with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:raw, :erb, :html, :builder, :ruby, :coffee, :haml, :jbuilder]}. Searched in:
* "/Users/koz/Desktop/forum/app/views"
* "/Users/koz/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/devise-4.2.0/app/views"
):
app/controllers/comments_controller.rb:11:in `create'
Rendering /Users/koz/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/missing_template.html.erb within rescues/layout
Rendering /Users/koz/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/_source.html.erb
Rendered /Users/koz/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/_source.html.erb (14.6ms)
Rendering /Users/koz/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb
Rendered /Users/koz/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (5.4ms)
Rendering /Users/koz/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb
Rendered /Users/koz/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (2.9ms)
Rendered /Users/koz/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/actionpack-5.0.0.1/lib/action_dispatch/middleware/templates/rescues/missing_template.html.erb within rescues/layout (297.8ms)
【问题讨论】:
-
可以添加评论模型文件吗?里面有哪些验证?
-
这里实际上有两个问题 - 一,您的 cmets 没有保存,二,您的
Comments#new视图没有被找到。解决第二个问题,可以试试把render 'new'改成render :new吗? -
我只是还没有创建名为“新”的视图
-
好的,很公平。不过,我们仍然需要查看模型文件来诊断您的第一个问题。
-
现在就做。给我一分钟:)
标签: html ruby-on-rails ruby database save