How to declare objects for nested form in rails 4 -


i'm approaching 2nd week of using rails, , have gotten structure of app complete (i think/hope). i'm building staff website charity, part of work collects , wipes computers (before sending them african schools!).

i've set app have staff, computer, , wipe models / classes, , nested form wiping details inside form computer details. when create new computer, can enter wiping details without problem, , if edit wiping details of existing computer that has wiping details works great. when go add wiping details existing computer without wiping details following error:

undefined method `staff=' nil:nilclass extracted source (around line #49): 

how correctly set computer.wipe.staff = current user. note edit or submit wiping details user must logged in.

my models follows (ignoring irrelevant validations, etc):

class staff < activerecord::base   has_many :wipes   before_save { self.staff_email = staff_email.downcase }   validates :staff_name, presence: true, length: { minimum: 5, maximum: 50}   has_secure_password end  class computer < activerecord::base   has_one :wipe, dependent: :destroy   accepts_nested_attributes_for :wipe end  class wipe < activerecord::base   belongs_to :computer#, dependent: destroy   belongs_to :staff   validates :staff_id, presence: true  #validates :computer_id, presence: true   validates :action_taken, presence: true, length: { minimum: 2, maximum: 250 } end 

my computer controller follows:

class computerscontroller < applicationcontroller   before_action :set_computer, only: [:edit, :update, :show]   before_action :require_user, except: [:new, :create]    layout :new_layout, only: [:new, :update]    def new     @computer = computer.new     @computer.build_wipe   end    def create     @computer = computer.new(computer_params)     if logged_in?       @computer.wipe.staff = current_user     end     if @computer.save       flash[:success] = "you're computer's details have been submitted successfully!"       redirect_to computers_path     else       render :new, layout: new_layout     end   end    def edit     unless @computer.wipe       @computer.build_wipe     end   end    def update     @computer.wipe.staff = current_user #this line not working, yet seems work correctly in create action     if @computer.update(computer_params)       flash[:success] = "the computer's details have been updated successfully!"       redirect_to computer_path(@computer)     else       render :edit     end   end    private      #whitelisting variables     def computer_params       params.require(:computer).permit(:manufacturer, :computer_type, :specification, :donor, :model_no, :serial_no, :product_key, :turingtrack, :picture, wipe_attributes: [:id, :action_taken, :staff_id])     end      def set_computer       @computer = computer.find(params[:id])     end  end 

my view is:

<div class="row">   <div class="well col-md-8 col-md-offset-2">     <%= form_for(@computer, html: { multipart: true }) |f| %>        <%= f.label :manufacturer %>       <%= f.text_field :manufacturer, :placeholder => "toshiba" %>       <%= f.label :computer_type, "computer type" %>       <%= f.text_field :computer_type, :placeholder => "laptop"%>        <% if logged_in? %>         <br><strong>wiping details:<br><br></strong>         <%= f.fields_for :wipe |wipe_form| %>           <%= wipe_form.label :action_taken %>           <%= wipe_form.text_field :action_taken %>         <% end %>       <% end %>        <%= f.submit(@computer.new_record? ? "submit details" : "submit edited details", class: "btn btn-success") %>     <% end %>    </div> </div> 

furthermore, not setting computer_id when using nested wipe form inside computer form, seems automatically assign (e.g. if created new computer/edit 1 , enter wiping details, computer_id wipe of computer created/edited). assumption correct? read somewhere not being able validate parent's foreign keys, brief understand. throws error when try validate computer_id in wipes model, hence validation being commented out. why this, , okay practice leave this?

thank everyone

based on error:

    undefined method `staff=' nil:nilclass. 

this shows @computer.wipe nil. while set_computer method correctly finding computer based on provided id, @computer has no associated wipe.

edit: related problem in build_wipe method not correctly settings wipe.


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -