我是 ruby on rails 的新手,我正在通过创建博客来学习。我无法保存到我的博客表中,并且收到此错误“无法写入未知属性 url”
博客迁移: db/migrate/
class CreateBlogs < ActiveRecord::Migration
def change
create_table :blogs do |t|
t.string :title
t.text :description
t.string :slug
t.timestamps
end
end
end
博客模型: /app/models/blogs.rb
class Blogs < ActiveRecord::Base
acts_as_url :title
def to_param
url
end
validates :title, presence:true
end
博客控制器: /app/controllers/blogs_controller.rb
class BlogsController < ApplicationController
before_action :require_login
def new
@blogs = Blogs.new
end
def show
@blogs = Blogs.find_by_url(params[:id])
end
def create
@blogs = Blogs.new(blogs_params)
if @blogs.save
flash[:success] = "Your Blog has been created."
redirect_to home_path
else
render 'new'
end
end
def blogs_params
params.require(:blogs).permit(:title,:description)
end
private
def require_login
unless signed_in?
flash[:error] = "You must be logged in to create a new Blog"
redirect_to signin_path
end
end
end
博客表单:/app/views/blogs/new.html.erb
块引用
<%= form_for @blogs, url: blogs_path do |f| %><br/>
<%= render 'shared/error_messages_blogs' %><br/>
<%= f.label :title %><br/>
<%= f.text_field :title %><br/>
<%= f.label :description %><br/>
<%= f.text_area :description %><br/>
<%= f.submit "Submit Blog", class: "btn btn-large btn-primary" %><br/>
<% end %><br/>
我还在我的 routes.rb 文件中添加了“资源:博客”。
我在控制器中收到此错误
如果@blogs.save