Mysql 使用另一个表的ID创建新表项
我正在尝试将用户id(以便我可以访问用户详细信息)附加到已附加到名为Park的页面的评论 我设置了三个表:Park、Comment和User:Mysql 使用另一个表的ID创建新表项,mysql,ruby-on-rails,ruby,devise,Mysql,Ruby On Rails,Ruby,Devise,我正在尝试将用户id(以便我可以访问用户详细信息)附加到已附加到名为Park的页面的评论 我设置了三个表:Park、Comment和User: class Park < ActiveRecord::Base has_many :comments has_many :users end class Comment < ActiveRecord::Base belongs_to :park has_one :user end class Use
class Park < ActiveRecord::Base
has_many :comments
has_many :users
end
class Comment < ActiveRecord::Base
belongs_to :park
has_one :user
end
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_many :comments
validates_presence_of :username
validates :username, uniqueness: true
def admin?
admin
end
end
我的评论控制器设置如下:
class CommentsController < ApplicationController
before_action :authenticate_user!, only: [:create]
before_action :authorize_admin!, only: [:delete]
def create
@park = Park.find(params[:park_id])
@comment = @park.comments.create(comment_params)
redirect_to park_path(@park)
end
def destroy
@park = Park.find(params[:park_id])
@comment = @park.comments.find(params[:id])
@comment.destroy
redirect_to park_path(@park)
end
private
def comment_params
params.require(:comment).permit(:comment, :user_id, :park_id)
end
end
params.require(:comment).permit(:comment, :park_id)
class CommentsController
我能够从一个park_id到另一个user_id来回摆动,但在另一个条件下,我总是得到一个大大的零
我只是试图在我的评论表单中添加一个隐藏字段
<%= form_for([@park, @park.comments.build]) do |f| %>
<p>
<%= f.label :comment %><br>
<%= f.text_area :comment %>
<%= hidden_field_tag(:user_id, current_user.id) %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
但这似乎没有产生任何结果
我尝试了一些加入ActiveRecord的工作,但它不想抓住我的创作,所以我放弃了。花了一段时间在这个问题上,我相信有一个简单的解决方案,我只是没有看到。想法?我不确定我是否正确理解了您的意思,但看看您的代码,我发现了一些问题 首先,创建一个隐藏字段,但不使用表单帮助器。这就是为什么您的用户id在您的
注释参数中不可见,从而导致
大胖零
:)。
尝试改变
<%= hidden_field_tag(:user_id, current_user.id) %>
您现在可以从注释参数中删除:user_id
,如下所示:
class CommentsController < ApplicationController
before_action :authenticate_user!, only: [:create]
before_action :authorize_admin!, only: [:delete]
def create
@park = Park.find(params[:park_id])
@comment = @park.comments.create(comment_params)
redirect_to park_path(@park)
end
def destroy
@park = Park.find(params[:park_id])
@comment = @park.comments.find(params[:id])
@comment.destroy
redirect_to park_path(@park)
end
private
def comment_params
params.require(:comment).permit(:comment, :user_id, :park_id)
end
end
params.require(:comment).permit(:comment, :park_id)
实际上,您甚至不需要:在那里放置id
您的隐藏字段标签
不起作用。它将创建一个单独的参数,而不是嵌套在所需的参数键下的参数
我的建议是完全删除隐藏的_字段,并在控制器中指定当前的_用户
class CommentsController < ApplicationController
def create
@park = Park.find(params[:park_id])
@comment = @park.
comments.
create(
comment_params.
merge(user_id: current_user.id) # adding here, on creation
# or merge(user: current_user)
)
redirect_to park_path(@park)
end
def comment_params
params.
require(:comment).
permit(:comment)
end
end
class CommentsController
为什么??因为否则,用户可能会更改该隐藏字段的值,并且您将错误地存储注释的用户id值
class CommentsController < ApplicationController
def create
@park = Park.find(params[:park_id])
@comment = @park.
comments.
create(
comment_params.
merge(user_id: current_user.id) # adding here, on creation
# or merge(user: current_user)
)
redirect_to park_path(@park)
end
def comment_params
params.
require(:comment).
permit(:comment)
end
end