我的应用程序中有3个模型。用户(通过Devise),发布和回复。 用户可以是帖子和回复的父级,回复属于用户和帖子。
由于某种原因,它不将答复保存到数据库。在输出中,我看不到 user_id 。我用来创建 @reply 的函数是 current_user.replies.create(reply_params)。 HTML输出括号也对我来说似乎有点奇怪。为什么在:reply 大括号中只有:comment ?知道我做错了什么吗?
型号:
it('setTimeMonth(number)', () => {
const wrapper = new ReactPage;
expect(wrapper.state('Date1').toMatch(""));
expect(wrapper.state('Date2').toMatch(""));
wrapper.setTimeMonth(1);
expect(wrapper.state('Date1').toMatch(moment().format('YYYY-MM-DD')));
expect(wrapper.state('Date2').toMatch(moment().subtract(1, 'Month').format('YYYY-MM-DD')));
});
控制器:
class Post < ActiveRecord::Base
belongs_to :user
has_many :replies
validates_presence_of :title, :tekst
validates :tekst, length: {minimum: 10}
end
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_many :posts, dependent: :destroy
has_many :replies
end
class Reply < ActiveRecord::Base
belongs_to :post
belongs_to :user
validates_presence_of :user, :post, :comment
validates :comment, length: {minimum: 10}
end
表格:
class RepliesController < ApplicationController
before_action :set_post, only: [:new, :index, :create]
before_filter :authenticate_user!
def index
@replies = @post.replies
end
def new
@reply = Reply.new
end
def create
@reply = current_user.replies.create(reply_params)
respond_to do |format|
if @reply.save
format.html { redirect_to @post, notice: 'Reply was
successfully saved' }
format.json { render json: @post }
else
format.html { render :new }
format.json { render json: @post.errors, status: 'Wrong entry'
}
end
end
end
private
def reply_params
params.require(:reply).permit(:comment, :post_id)
end
def set_post
@post = Post.find(params[:post_id])
end
end
模式:
<%= form_for [@post, @reply] do |f| %>
<%= f.text_field :comment %>
<% f.hidden_field :post_id, value: @post.id %>
<%= f.submit %>
<% end %>
以及输出的外观:
create_table "replies", force: :cascade do |t|
t.integer "post_id"
t.integer "user_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.text "comment"
end
add_index "replies", ["post_id"], name: "index_replies_on_post_id"
add_index "replies", ["user_id"], name: "index_replies_on_user_id"
答案 0 :(得分:1)
失败,因为您没有将帖子与回复相关联。试试这个...
java.security.InvalidAlgorithmParameterException: Prime size must be multiple of 64, and can only range from 512 to 1024 (inclusive)
at com.sun.crypto.provider.DHKeyPairGenerator.initialize(DHKeyPairGenerator.java:120)
at java.security.KeyPairGenerator$Delegate.initialize(Unknown Source)
此外,请注意,您应该实例化答复(使用def create
@reply = @post.replies.new(reply_params)
@reply.user = current_user
respond_to do |format|
if @reply.save
...
end
end
,而不是new
),然后保存它。