Rails 4.1.8 "blog" 使用强参数时帖子不保存文本或标题

Rails 4.1.8 "blog" posts not saving text or title when using strong parameters

我潜伏了很长时间,google-fu 今天让他失望了。我一直在学习使用 Rails 使用 Rails 4 发布之前创建的视频系列。因此,讲座使用 attr_accessibly 批量作业。我一直在尝试将其转换为有效的强参数,但实际上在将信息保存到我的 PostgreSQL 数据库时遇到了问题。

基本上,我应该去localhost:3000/new,添加标题,body,和类别,然后提交。这一切顺利,但我的 /posts(列出所有 posts)将标题显示为 /posts/(无论它在什么行号),不显示文本,并且仅在单击 post (url) 时显示 date-time 戳记。另外,我的数据库只存储了一个 post/row 数字。

注意:我知道 ActiveAdmin 之类的东西,但更愿意在使用此类模块之前学习如何手动 make/save posts。

这里是 post 控制器:

class PostsController < ApplicationController
   def index
   @posts = Post.all
end

def show
   @post = Post.find(params[:id])
end

def new
  @post = Post.new
  @category = Category.all
end

def create
 @post = Post.create(post_params)
  if @post.save
    redirect_to posts_path, :notice => "Your post has been saved"

  else

    render "new"
  end
end

def edit

end

def update

end

def destroy

end

private  
   def post_params
   params.require(:post).permit(:title, :body, :category_id, :author_id)


end

end

这是 html 表格:

<h1>Add New Post</h1>
<%= form_for @post do |f| %>

    <p>
      <%= f.label :title %><br />
      <%= f.text_field :title %><br />
    </p>

    <p>
      <%= f.label :body %><br />
      <%= f.text_area :body %><br />
    </p>
    <p>
      <%= f.select :category_id, Category.all.collect {|x| [x.name, x.id]},     {:include_blank=> "Select One"}%><br />
 </p>
<p>
  <%= f.submit "Add Post" %>
</p>
<% end %>

最后,posts.rb 文件:

class Post < ActiveRecord::Base
   #   Deprecated
   # attr_accessible :title, :body, :category_id, :author_id

   belongs_to :category
   accepts_nested_attributes_for :category
end

我不是 Ruby 专家,所以我的第一个想法是 .save 方法在使用 strong_parameters 时需要额外的参数。将 (post_params) 添加到保存方法似乎没有效果,而且我无法使用 Ruby 文档确定我的问题。我更愿意以 "right" 方式执行此操作,而不是仅使用 protected_attributes gem 来使用已弃用(看似不太安全)的方法。

提前感谢您提供的任何帮助。等我有钱了,给你买辆法拉利*。

*可能是也可能不是 Hot Wheels 的复制品。

Eidt 2:这是服务器在尝试 POST 到我的数据库时抛出的内容。它提到了质量分配错误,但我认为我没有使用它们(而是强参数)。请原谅我的新手:

Started POST "/posts" for 127.0.0.1 at 2015-03-11 18:28:13 -0700
Processing by PostsController#create as HTML
Parameters: {"utf8"=>"✓",    "authenticity_token"=>"9Ybchogw5u+sYbZOFYZtbJbXBkWC5EuCIZNUmAKyAQI=", "post"=>  {"title"=>"blahblahblah", "body"=>"blahblahblah", "category_id"=>"1"},   "commit"=>"Add Post"}
WARNING: Can't mass-assign protected attributes for Post: title, body,   category_id
app/controllers/posts_controller.rb:18:in `create'
[1m[36m (0.0ms)[0m  [1mBEGIN[0m
[1m[35mSQL (1.0ms)[0m  INSERT INTO "posts" ("created_at", "updated_at")  VALUES (, ) RETURNING "id"  [["created_at", "2015-03-12 01:28:13.990971"],   ["updated_at", "2015-03-12 01:28:13.990971"]]
[1m[36m (2.0ms)[0m  [1mCOMMIT[0m
[1m[35m (0.0ms)[0m  BEGIN
[1m[36m (0.0ms)[0m  [1mCOMMIT[0m
Redirected to http://localhost:3000/posts
Completed 302 Found in 10ms (ActiveRecord: 3.0ms)

和索引视图。这是一个训练练习,所以它只是文本,没有样式:

<h1>Blog Posts</h1>
<% @posts.each do |post| %>
    <h3><%= link_to post.title, post %></h3>
    <p><%= post.body %></p>
    <% end %>

取消注释 post_params 中的 request/permit 行,并实际将参数名称添加到其中

def post_params
   params.require(:post).permit(:title, :body, :category_id, :author_id)
end

这就是为什么您的帖子中没有数据的原因 - 因为您不再从参数中获取任何数据。

如果 permit/require 行给您带来了不同的错误 - 我们会帮助您解决这个问题,但注释掉安全措施并不是解决问题的方法。