我按照Youtube视频在我的Rails应用程序中实现了ActiveAdmin主题,-一切都像魅力一样工作(我想)。
https://www.youtube.com/watch?v=i2x995hm8r8
我遵循了他采取的每一步,我现在有点困惑,因为我无法创建帖子。 每当我尝试创建新帖子并输入磁贴、正文并选择图像时 - 它不会做任何事情。它甚至没有给我错误消息。
posts_controller.rb
class PostController < ApplicationController
def index
@post = Post.all.order('created_at DESC')
end
def create
@post = Post.new(params[:post].permit(:title, :body))
if @post.save
redirect_to @post
else
render 'new'
end
end
def show
@post = Post.find(params[:id])
@post = Post.order("created_at DESC").limit(4).offset(1)
end
def edit
@post = Post.find(params[:id])
end
def update
@post = Post.find(params[:id])
if @post.update(params[:post].permit(:title, :body))
redirect_to @post
else
render 'edit'
end
end
def destroy
@post = Post.find(params[:id])
@post.destroy
redirect_to posts_path
end
private
def post_params
params.require(:post).permit(:title, :body)
end
end
ActiveAdmin.register Post do
帖子.rb
permit_params :title, :body, :image
show do |t|
attributes_table do
row :title
row :body
row :image do
post.image? ? image_tag(post.image.url, height: '100') : content_tag(:span, "No image yet")
end
end
end
form :html => {:multipart => true} do |f|
f.inputs do
f.input :title
f.input :body
f.input :image, hint: f.post.image? ? image_tag(post.image.url, height: '100') : content_tag(:span, "Upload JPG/PNG/GIF image")
end
f.actions
end
end
后.rb
class Post < ApplicationRecord
belongs_to :user
validates :title, presence: true, length: {minimum: 5}
validates :body, presence: true, length: { maximum: 140}
has_attached_file :image, styles: { medium: "300x300>", thumb: "100x100>" }
validates_attachment_content_type :image, content_type: /Aimage/.*z/
end
编辑一:
Started POST "/admin/posts" for 127.0.0.1 at 2018-03-20 14:30:24 +0100
Processing by Admin::PostsController#create as HTML
Parameters: {"utf8"=>"✓",
"authenticity_token"=>"f+hfBD3lzgXEfz1q38/i3YciHsbb5LYWbbHUUsyIeOCaNSUReUUVVTBE//Dw0zXxSuFCzcMfYuUGDtIJlNb58w==", "post"=>{"title"=>"asdasdasd", "body"=>"asdasdasd"}, "commit"=>"Create Post"}
AdminUser Load (0.3ms) SELECT "admin_users".* FROM "admin_users" WHERE "admin_users"."id" = $1 ORDER BY "admin_users"."id" ASC LIMIT $2 [["id", 1], ["LIMIT", 1]]
(0.2ms) BEGIN
(0.1ms) ROLLBACK
Rendering /Users/useruser/.rvm/gems/ruby-2.4.2/bundler/gems/activeadmin-2cf85fb03ab3/app/views/active_admin/resource/new.html.arb
Rendered /Users/useruser/.rvm/gems/ruby-2.4.2/bundler/gems/activeadmin-2cf85fb03ab3/app/views/active_admin/resource/new.html.arb (134.6ms)
Completed 200 OK in 230ms (Views: 148.3ms | ActiveRecord: 5.7ms)
如果你需要更多的我的代码,告诉我我应该在这里发布什么。 我刚刚开始使用Ruby on Rails和整体编程,所以是的,我确实是一个新手。
提前感谢!
从我在编辑 1 中看到的内容来看,我看到您在提交表单后呈现new
。这意味着您的帖子未被保存。这也意味着您的应用程序完全执行它应该执行的操作。
我假设您使用的是最新的 Rails 5。
在帖子模型中,您有belongs_to
关联(帖子属于用户)。 在 Rails 5 中,这意味着必须提供user
或user_id
才能创建 Post(Post 不能属于任何人),否则您将无法保存。
根据您在表中创建关联的方式,您也许能够在参数中传递user
或user_id
。
创建属于特定用户的帖子的另一种方法是:
@user = User.first
@post = @user.posts.build(post_params)
对于 ActiveAdmin,您可以使用基于您的模型创建的默认窗体。 只需确保在以这种方式创建时允许所有参数
ActiveAdmin.register Post do
permit_params %i[title body image user_id]
...
end
您还可以belongs_to :user
关联设置为可选。
现在我的一些一般建议:
首先使用适当的缩进。
我给你的建议是安装Rubocop gem。
第二:
def show
@post = Post.find(params[:id])
@post = Post.order("created_at DESC").limit(4).offset(1)
end
这没有多大意义,您在第一次赋值后就覆盖了实例变量。@post = Post.order("created_at DESC").limit(4).offset(1)
更像是一个索引操作,因为它不显示特定的帖子,它显示 2..5 个最新帖子。
def post_params
params.require(:post).permit(:title, :body)
end
错过image
属性。
def update
@post = Post.find(params[:id])
if @post.update(params[:post].permit(:title, :body))
redirect_to @post
else
render 'edit'
end
end
您复制params[:post].permit(:title, :body)
.您已经为此创建了私有方法。在这里使用它。创建操作也是如此,您也在那里复制了它。阅读 DRY 代码是关于什么的(谷歌它)。
您belongs_to :user
但从未设置user_id
。默认情况下需要belongs_to
关系(在最近的 rails 4/5 中),这将阻止保存。目前,解决此问题的最简单方法是将其编写如下
belongs_to :user, optional: true
[编辑:如何将当前用户存储为帖子的所有者]
如果要自动将用户设置为当前登录的用户(我认为这是意图),则可以将以下内容添加到您的活动管理员配置中:
ActiveAdmin.register Post do
# .. keep your original configuration here ...
before_build do |record|
record.user = current_user
end
end
或者向表单添加一个额外的字段,允许管理员选择用户?