![](/img/trans.png)
[英]Random ActiveRecord::StatementInvalid errors on Rails 4 app on Heroku
[英]Heroku + Rails Error ActiveRecord::StatementInvalid
我在這里遇到了一些困境。
我有一個評論腳手架和一個電影腳手架
當去電影頁面並創建新評論時,我去這里/ movies / 12 / comments / new(12是id)
並且做了我的控制器
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時,我得到了這個錯誤
*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一起使用? ,但仍然沒有運氣
有任何想法嗎? 謝謝!
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
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
class Comment < ActiveRecord::Base
attr_accessible :body, :movie_id, :subject, :user_id
belongs_to :user
belongs_to :movie
end
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
正如您傳遞的那樣, 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
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.