NewbieHelp


L

Luis Teko

Im having n error message

ActiveRecord::AssociationTypeMismatch in MovieController#create

Genre expected, got String

Parameters:

{"commit"=>"Create",
"authenticity_token"=>"b2aa33146f0a40aa59d887b8e19d0635cbdc2612",
"movie"=>{"title"=>"Matrix",
"price"=>"15",
"description"=>"sldljsvnsdvdv",
"genre"=>"1"}}

<h1>Add new movie</h1>
<%= form_tag :action => 'create' %>
<p><label for="movie_title">Title</label>:
<%= text_field 'movie', 'title' %></p>
<p><label for="movie_price">Price</label>:
<%= text_field 'movie', 'price' %></p>
<p><label for="movie_genre">Genre</label>:
<%= collection_select:)movie,:genre,@genres,:id,:name) %></p>
<p><label for="movie_description">Description</label><br/>
<%= text_area 'movie', 'description' %></p>
<%= submit_tag "Create" %>

class MovieController < ApplicationController
def list
@movies = Movie.find:)all)
end

def show
@movie = Movie.find(params[:id])
end

def new
@movie = Movie.new
@genres = Genre.find:)all)
end

def create
@movie = Movie.new(params[:movie])
if @movie.save
redirect_to :action => 'list'
else
@genres = Genre.find:)all)
render :action => 'new'
end
end

def edit
@movie = Movie.find(params[:id])
@genres = Genre.find:)all)
end

def update
@movie = Movie.find(params[:id])
if @movie.update_attributes(params[:movie])
redirect_to :action => 'show', :id => @movie
else
@genres = Genre.find:)all)
render :action => 'edit'
end
end

def delete
Movie.find(params[:id]).destroy
redirect_to :action => 'list'
end


end
model for movie

class Movie < ActiveRecord::Base
belongs_to :genre

end
model for genre

class Genre < ActiveRecord::Base
has_many :movies
end


 
Ad

Advertisements

K

Kai König

For AR movie.genre is an object of type Genre. Also on creation it
expects move.genre to be an Object of the type Genre e.g. your Model.
so the make this work quickly:

@movie = Movie.new(params[:movie])
@movie.genre = Genre.find :condition => { :id =>
params[:genre_id] }, :limit => 1

or

You could take a look at nested forms
http://ryandaigle.com/articles/2008/7/19/what-s-new-in-edge-rails-nested-models



Im having n error message

ActiveRecord::AssociationTypeMismatch in MovieController#create

Genre expected, got String

Parameters:

{"commit"=>"Create",
"authenticity_token"=>"b2aa33146f0a40aa59d887b8e19d0635cbdc2612",
"movie"=>{"title"=>"Matrix",
"price"=>"15",
"description"=>"sldljsvnsdvdv",
"genre"=>"1"}}

<h1>Add new movie</h1>
<%= form_tag :action => 'create' %>
<p><label for="movie_title">Title</label>:
<%= text_field 'movie', 'title' %></p>
<p><label for="movie_price">Price</label>:
<%= text_field 'movie', 'price' %></p>
<p><label for="movie_genre">Genre</label>:
<%= collection_select:)movie,:genre,@genres,:id,:name) %></p>
<p><label for="movie_description">Description</label><br/>
<%= text_area 'movie', 'description' %></p>
<%= submit_tag "Create" %>

class MovieController < ApplicationController
def list
@movies = Movie.find:)all)
end

def show
@movie = Movie.find(params[:id])
end

def new
@movie = Movie.new
@genres = Genre.find:)all)
end

def create
@movie = Movie.new(params[:movie])
if @movie.save
redirect_to :action => 'list'
else
@genres = Genre.find:)all)
render :action => 'new'
end
end

def edit
@movie = Movie.find(params[:id])
@genres = Genre.find:)all)
end

def update
@movie = Movie.find(params[:id])
if @movie.update_attributes(params[:movie])
redirect_to :action => 'show', :id => @movie
else
@genres = Genre.find:)all)
render :action => 'edit'
end
end

def delete
Movie.find(params[:id]).destroy
redirect_to :action => 'list'
end


end
model for movie

class Movie < ActiveRecord::Base
belongs_to :genre

end
model for genre

class Genre < ActiveRecord::Base
has_many :movies
end


 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top