如何将评论与用户相关联。我的用户控制器是
class UsersController < ApplicationController
def new
end
def create
user = User.new(user_params)
if user.save
session[:user_id] = user.id
redirect_to '/url'
else
redirect_to '/signup'
end
end
def user_params
params.require(:user).permit(:name, :email, :password, :password_confirmation)
end
end
我的网址控制器是
class UrlsController < ApplicationController
def new
end
def create
url = Url.new(url_params)
url.save
redirect_to @url
end
def url_params
params.require(:url).permit(:url)
end
end
我在url_params
中遇到错误。文本字段应该如何?
答案 0 :(得分:0)
例如,您需要创建评论模型
class Comment < ApplicationRecord
belongs_to :user
end
class User < ApplicationRecord
has_many :comments
end
并创建CommentsController
class CommentsController < ApplicationController
before_action :set_user
def create
@user.comments.create(comment_params)
redirect_to root_url
end
private
def set_user
@user = User.find(session[:user_id])
end
def comment_params
params.require(:comment).permit(:text)
end
end