2013-06-11 163 views
0

我在这里遇到了一些困难。Heroku + Rails错误ActiveRecord :: StatementInvalid

我有一个评论的支架和电影脚手架

当看一场电影页面并创建一个新的评论,我去这里/电影/ 12 /评论/新(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,这里Why does this :id in Rails not work with Postgresql but it does work with 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 
+0

看起来'movie_id'实际上是“the-social-network”,它不是一个整数。 –

+0

你能发表你的看法吗?这个问题很可能存在于你的表单中。 – zeantsoi

+0

您的视图看起来正确,但是':movie_id'传递的是一个URL类型的字符串,而不是一个id类型的整数。你可以同时发布'Movie'和'Comment'模型吗? – zeantsoi

回答

2

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

某些东西在您的视图中可能配置错误。确保你通过movie.id而不是别的。

编辑:接收

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

+0

好吧,我如何改变movie_id来传递参数呢? – PMP

+0

嗯......你可以进入你的Rails控制台并发布一个现有的'Comment'对象的例子吗?我很想看看'movie_id'属性是什么。从命令行输入'rails console',然后输入'Comment.first'。回顾它返回的内容。 – zeantsoi

+0

评论加载(0.8ms)SELECT“comments”。* FROM“comments”LIMIT 1 =># PMP

相关问题