0

after the community an i solved my problem rails how to use Associated Model with the admin namespace i get the data from the form into the controller, my def create crashed.

I'm using rails 5.1.5 with ruby 2.5.0. I have two models the model A and Model B.

Model A has_many bs
Model B belongs_to a

The Controller with the crash is

app/controllers/admin/bs_controller.rb

class Admin::BsController < ApplicationController

def create
  @a = A.find(params[:a_id])
  @b = @a.bs.create(entry_params)
  redirect_to a_path(@a)
end

NoMethodError in Admin::BsController#create

undefined method `bs' for nil:NilClass

def create
  @a = A.find(params[:a_id])
  **@b = @a.bs.create(entry_params)**
  redirect_to a_path(@a)
end

Is it correct? First i have to find the "A-Entry" from is main Model, the i can create the new entry which belongs_to a?

amarradi
  • 109
  • 3
  • 16
  • 2
    Just make sure the result of `A.find(params[:wishlist_id])` isn't nil. Use a debugger, check your strong params, look for any error. – Sebastián Palma Mar 20 '18 at 18:44
  • Thats right! It isn't nil. But i changed the id-handling via to_param in my model and now i need an other id because i'm using an other id. – amarradi Mar 20 '18 at 20:56
  • It can't be `nil`, because `#find` raises an exception if it doesn't find the record, suggesting the code posted is not the one running when the error happens. – Drenmi Mar 20 '18 at 21:19
  • now i change the to_param method that the id i use. But there is an error that i've never seen before `undefined method `string' for # Did you mean? String` – amarradi Mar 20 '18 at 21:59

0 Answers0