繁体   English   中英

Rails评论用户+ ID

[英]Rails Comments User + Id

在我的Rails应用程序中,我有一个评论支架,可让用户对电影发表评论。

我面临两个问题。

第一个问题是,即使没有登录,任何人都可以创建评论,我将如何为用户分配评论,因此,如果有current_user ,他们可以创建评论,并且我可以分配用户注释,因此<%= comment.user.first_name%>,如果未登录,则无法创建注释。 我该怎么办? (我正在使用devise)

第二个问题是,当我创建评论时,它将带我到此路径(其中12是:movie_id)

localhost:3000/movies/12/comments/new

很好,但是当我创建评论时,我必须指定movie_id(12),这是如何自动完成的,因此Rails看到评论的movie_id为12。

我的评论控制器,万一需要

class CommentsController < ApplicationController
  # GET /comments
  # GET /comments.json
  before_filter :load_movie


  def index
    @comments = @movie.comments.all
    @search = Movie.search(params[:q])

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @comments }
    end
  end

  # GET /comments/1
  # GET /comments/1.json
  def show
    @comment = Comment.find(params[:id])
    @search = Movie.search(params[:q])

    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @comment }
    end
  end

  # GET /comments/new
  # GET /comments/new.json
  def new
    @comment = Comment.new
    @search = Movie.search(params[:q])

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @comment }
    end
  end

  # GET /comments/1/edit
  def edit
    @comment = Comment.find(params[:id])
    @search = Movie.search(params[:q])

  end

  # POST /comments
  # POST /comments.json
  def create
    @comment = Comment.new(params[:comment])
    @search = Movie.search(params[:q])

    respond_to do |format|
      if @comment.save
        format.html { redirect_to :back }
        format.json { render json: @comment, status: :created, location: @comment }
      else
        format.html { render action: "new" }
        format.json { render json: @comment.errors, status: :unprocessable_entity }
      end
    end
  end

  # PUT /comments/1
  # PUT /comments/1.json
  def update
    @comment = Comment.find(params[:id])
    @search = Movie.search(params[:q])

    respond_to do |format|
      if @comment.update_attributes(params[:comment])
        format.html { redirect_to (@movie), notice: 'Comment was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: "edit" }
        format.json { render json: @comment.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /comments/1
  # DELETE /comments/1.json
  def destroy
    @comment = Comment.find(params[:id])
    @comment.destroy

    respond_to do |format|
      format.html { redirect_to comments_url }
      format.json { head :no_content }
    end
  end

private
    def load_movie
      @movie = Movie.find_by_id(:movie_id)
    end


end

第一:
使用devise,您可以通过在控制器顶部说一句来请求用户登录:

before_filter :authenticate_user!, only: [:new,:create]

因此,如果未登录的用户尝试访问这些操作,则会将其重定向到登录页面,并在登录后转发到原始请求。

第二:
从路由中可以看到,将12分配给params [:movie_id]。 所以,在你的控制器new的行动写:

@movie = Movie.find(params[:movie_id])
@comment = @movie.comments.new
@comment.user=current_user 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM