【问题标题】:Rails Comments User + IdRails 评论用户 + Id
【发布时间】:2013-06-04 19:17:10
【问题描述】:

在我的 Rails 应用程序中,我有一个 cmets 脚手架,可以让用户评论电影。

我面临两个问题。

第一个问题是任何人都可以创建评论,即使他们没有登录,我如何将评论分配给用户,所以如果有current_user,他们可以创建评论,我会能够将用户分配给评论,所以 ,如果他们没有登录,他们就不能创建评论。我该怎么做? (我正在使用设计)

第二个问题是,当我创建评论时,它会将我带到这条路径(其中 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

【问题讨论】:

    标签: ruby-on-rails devise comments


    【解决方案1】:

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

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

    因此,如果有人未登录尝试访问这些操作,他们将被重定向到登录页面,并在登录后转发到原始请求。

    第二:
    从路由中可以看出,12 被分配给 params[:movie_id]。所以在你的控制器new action write:

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

    【讨论】:

    • 太棒了,谢谢一切正常,除了 我得到未定义的方法 `first_name'
    • 好吧,添加用户评论(我扩展了我的答案)
    • 我仍然得到错误 =>> undefined method `first_name'
    • 错误是undefined method 'first_name' for &lt;some_object&gt;。显示的对象是什么?
    猜你喜欢
    • 1970-01-01
    • 2013-06-02
    • 1970-01-01
    • 2015-04-05
    • 2013-06-08
    • 1970-01-01
    • 2014-01-23
    • 2012-09-15
    • 1970-01-01
    相关资源
    最近更新 更多