简体   繁体   English

Heroku + Rails错误ActiveRecord :: StatementInvalid

[英]Heroku + Rails Error ActiveRecord::StatementInvalid

I'm having a bit of a dilemma here. 我在这里遇到了一些困境。

I have a Comments scaffold and a Movies scaffold 我有一个评论脚手架和一个电影脚手架

When going to a movie page and creating a new comment, i go here /movies/12/comments/new (12 being the id) 当去电影页面并创建新评论时,我去这里/ movies / 12 / comments / new(12是id)

And did is my controller 并且做了我的控制器

def new
    @movie = Movie.find(params[:movie_id])
    @comment = @movie.comments.new 
    @search = Movie.search(params[:q])

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

But when i launch to heroku I get this error 但是当我启动到heroku时,我得到了这个错误

*Processing by CommentsController#new as HTML
 ActiveRecord::StatementInvalid (PG::Error: ERROR:  invalid input syntax for integer: "movie_id"

 Completed 500 Internal Server Error in 636ms
LINE 1: ...  "movies".* FROM "movies"  WHERE "movies"."id" = 'movie_id'...

Parameters: {"movie_id"=>"the-social-network"}
app/controllers/comments_controller.rb:99:in `load_movie'*

I tried removing the @movie = Movie.find(params[:movie_id]) or adding .to_i at the end, as stated here Why does this :id in Rails not work with Postgresql but it does work with MySQL? 我尝试删除@movie = Movie.find(params[:movie_id])或在末尾添加.to_i ,如此处所述为什么这样:Rails中的id不能与Postgresql一起使用,但它确实可以与MySQL一起使用? , but still no luck ,但仍然没有运气

Any ideas? 有任何想法吗? Thanks! 谢谢!

Comment_form (view) Comment_form(查看)

<%= simple_form_for(@comment) do |f| %>
  <%= f.error_notification %>

  <div class="form-inputs">
    <%= f.input :subject %>
    <%= f.input :body %>
    <%= f.input :movie_id %>
  </div>

  <div class="form-actions">
    <%= f.button :submit %>
  </div>
<% end %>

Movie.rb Movie.rb

class Movie < ActiveRecord::Base
  attr_accessible :title


 has_many :comments, :dependent => :destroy

      extend FriendlyId
  friendly_id :titleyear, use: :history
  def titleyear
    "#{title} #{id}"
  end

end

Comment.rb Comment.rb

class Comment < ActiveRecord::Base
  attr_accessible :body, :movie_id, :subject, :user_id

  belongs_to :user
  belongs_to :movie




end

Comments_controller.rb Comments_controller.rb

class CommentsController < ApplicationController
  # GET /comments
  # GET /comments.json
  before_filter :load_movie
  before_filter :authenticate_user!, only: [:new,:create,:edit,:destroy]



  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
    @movie = Movie.find(params[:movie_id])
    @comment = @movie.comments.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

The movie_id parameter, as you're passing it, is not actually an id , but a string: Parameters: {"movie_id"=>"the-social-network"} . 正如您传递的那样, movie_id参数实际上不是id ,而是字符串: Parameters: {"movie_id"=>"the-social-network"}

Something is probably misconfigured in your view. 您的视图中可能存在错误配置的内容。 Ensure that you're passing the movie.id and not something else. 确保您传递的是movie.id而不是其他内容。

EDIT: 编辑:

The error being received is indicating that the string the-social-network is being passed from the movie_id URL segment. 收到的错误表示正在从movie_id URL段传递字符串the-social-network You need to access your form by visiting a path that passes in a number as the second parameter, like http://yoursite/movies/2/comments/new , rather than http://yoursite/movies/the-social-network/comments/new . 您需要通过访问传递号码的路径作为第二个参数来访问您的表单,例如http://yoursite/movies/2/comments/new ,而不是http://yoursite/movies/the-social-network/comments/new

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

相关问题 Heroku上的Rails 4应用上的随机ActiveRecord :: StatementInvalid错误 - Random ActiveRecord::StatementInvalid errors on Rails 4 app on Heroku 带有Rails的敏捷Web开发-ActiveRecord :: StatementInvalid错误 - Agile Web Development with Rails - ActiveRecord::StatementInvalid Error ActiveRecord :: StatementInvalid:PG :: UndefinedTable:错误:在Rails查询中 - ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR: in rails query Rails抛出ActiveRecord :: StatementInvalid PG :: NotNullViolation:错误 - Rails throwing ActiveRecord::StatementInvalid PG::NotNullViolation: ERROR Rails:ActiveRecord :: StatementInvalid in - Rails: ActiveRecord::StatementInvalid in Heroku上的Rails:ActiveRecord :: StatementInvalid,重置和迁移不起作用 - Rails on Heroku: ActiveRecord::StatementInvalid, resetting and migrating doesn't work rails 4 ActiveRecord :: StatementInvalid - rails 4 ActiveRecord::StatementInvalid ActiveRecord::StatementInvalid 在 Rails 4.2.7.1 中 - ActiveRecord::StatementInvalid in Rails 4.2.7.1 Heroku-ActiveRecord :: StatementInvalid(PG :: Error:ERROR:列“ requested”不存在 - Heroku - ActiveRecord::StatementInvalid (PG::Error: ERROR: column “requested” does not exist 在heroku上插入数据库错误但在本地工作,ActiveRecord :: StatementInvalid - insert to database error on heroku but worked locally, ActiveRecord::StatementInvalid
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM