ruby on rails - NoMethodError in Characters#new -
i new rails.i creating rails project in product , character models.i have following questions.
q1. given association between 2 models right?
class product < activerecord::base has_many :characters end class character < activerecord::base belongs_to :product end q2. created link add new character in 'show' page of products such if click add new character should display 'new' page of characters in should have dropdown list of character names(i.e., height,width,weight , color) not.
it showing nomethoderror in characters#new error.
error raised in below line of characters/new file.
collection_select(:product, :c_id, @character, :id, :name) note: had created values name attribute height,weight,width,color in characters before migrating it.
q3. if works(i.e., q2), want show character name , value in 'show page of products.for how can redirect 'show' page of products..?
my characterscontroller new,show , create:
def show @product = product.find(params[:id]) @character = character.find(params[:id]) end def new @character = character.new(params[:character]) @product = product.find(params[:id]) end def create @character = character.new(params[:character]) if @character.save redirect_to :action => 'show', :id => @product.id else render :action => 'new' end end well now, after entering values character in characters/new , clicking create button trowing following error
activerecord::recordnotfound in productscontroller#show couldn't find product without id i want show character name , value in products/show. above error stopping me that..
my show method in productscontroller:
def show @product = product.find(params[:id]) @character = character.find(:all) end
- the classes correct reflect one-to-many relationship.
- problem nomethoderror appears related how setup model, routes, controller & view.
lets in model have attr_accessible :id, :name (i don't see in model , why error!)
in character controller need this:
def new @character = character.new @product = product.find(params[:character_id]) end in view(app/views/products/new.html.erb), can add link new characters as: <% link_to "character", new_product_character_path(product) %>
above assumes, set routes correct having following in config/routes.rb: "resources :products" "resources :characters"
Comments
Post a Comment