0

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)

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

*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?, but still no luck

Any ideas? Thanks!

Comment_form (view)

<%= 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
Community
  • 1
  • 1
PMP
  • 231
  • 6
  • 25
  • It appears that `movie_id` is actually "the-social-network" which is not an integer. – Mitch Dempsey Jun 11 '13 at 20:59
  • Can you post your view? The problem more than likely resides in your form. – zeantsoi Jun 11 '13 at 21:00
  • Your view looks correct, but it's unusual that `:movie_id` is passing a URL-type string, rather than an id-type integer. Can you post both the `Movie` and `Comment` models? – zeantsoi Jun 11 '13 at 21:09
  • Ok added the two files – PMP Jun 11 '13 at 21:51
  • Can you confirm you're visiting a URL like `http://hostname/movies/12/comments/new`, and not something like `http://hostname/the-social-network/comments/new`? – zeantsoi Jun 11 '13 at 22:35
  • 2013-06-11T20:46:23.804299+00:00 heroku[router]: at=info method=GET path=/movies/the-social-network/comments/new host=www.example.com fwd="00.000.000.000" dyno=web.1 connect=0ms service=1335ms status=500 bytes=643 – PMP Jun 11 '13 at 22:37
  • I changed the code for safety and confidential reasons Example.com is the domain name and 00.000.000.000 is my ip – PMP Jun 11 '13 at 22:38
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/31614/discussion-between-zeantsoi-and-user2225129) – zeantsoi Jun 11 '13 at 22:38

1 Answers1

2

The movie_id parameter, as you're passing it, is not actually an id, but a string: 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.

EDIT:

The error being received is indicating that the string the-social-network is being passed from the movie_id URL segment. 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.

zeantsoi
  • 25,857
  • 7
  • 69
  • 61
  • Ok so how would i change movie_id to pass a parameter instead? – PMP Jun 11 '13 at 21:04
  • Hmm... can you go into your Rails console and post an example of an existing `Comment` object? I'd be interested to see what the `movie_id` attribute is. From command line, type `rails console`, then `Comment.first`. Comment back with what it returns. – zeantsoi Jun 11 '13 at 22:11
  • Comment Load (0.8ms) SELECT "comments".* FROM "comments" LIMIT 1 => # – PMP Jun 11 '13 at 22:18
  • One last thing: can you post your `create` action for your `comments_controller.rb`? I think we're close to isolating the issue... – zeantsoi Jun 11 '13 at 22:22