Heroku Rails错误ActiveRecord :: StatementInvalid

Heroku + Rails Error ActiveRecord::StatementInvalid

我在这里有两难选择。

我有一个评论支架和一个电影支架

当进入电影页面并创建新评论时,我在这里/ movies / 12 / comments / new(12为ID)

那是我的控制器

1
2
3
4
5
6
7
8
9
10
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

但是当我启动到heroku时,出现此错误

1
2
3
4
5
6
7
8
*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'*

我尝试在最后删除@movie = Movie.find(params[:movie_id])或添加.to_i,如此处所述为什么在Rails中的:id在Postgresql中不起作用,但在MySQL中却起作用?但是仍然没有运气

有什么想法吗?
谢谢!

评论表(视图)

1
2
3
4
5
6
7
8
9
10
11
12
13
<%= simple_form_for(@comment) do |f| %>
  <%= f.error_notification %>

 
    <%= f.input :subject %>
    <%= f.input :body %>
    <%= f.input :movie_id %>
 

 
    <%= f.button :submit %>
 
<% end %>

Movie.rb

1
2
3
4
5
6
7
8
9
10
11
12
13
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

1
2
3
4
5
6
7
8
9
10
class Comment < ActiveRecord::Base
  attr_accessible :body, :movie_id, :subject, :user_id

  belongs_to :user
  belongs_to :movie




end

Comments_controller.rb

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
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


您传递的movie_id参数实际上不是id,而是字符串:Parameters: {"movie_id"=>"the-social-network"}

您的视图中可能配置有误。确保您传递的是movie.id,而不是其他。

编辑:

所接收到的错误指示正在从movie_id URL段传递字符串the-social-network。您需要通过访问以数字作为第二个参数的路径来访问表单,例如http://yoursite/movies/2/comments/new,而不是http://yoursite/movies/the-social-network/comments/new