0
votes

I keep getting this message #<ActionController::ParameterMissing: param is missing or the value is empty: property>, and just don't know how to resolve depsite all the research I have done and compared my work to.

I think I have my create action setup properly

def create
        raise params.inspect    
        @property = Property.create(prop_params)
       
         
        if @property.save!
            render json: @property
        else
            render json: { error: "Couldn't save"}
        end
    end

 def prop_params
        params.require(:property).permit(:address, :state, :sale_price, :owner_id)
 end

Here is my model as well for properties.

class Property < ApplicationRecord
    belongs_to :owner
    
end

When I do a raise param.inspect I get this

Object { status: 500, error: "Internal Server Error", exception: "#<RuntimeError: <ActionController::Parameters {\"controller\"=>\"properties\", \"action\"=>\"create\"} permitted: false>>", traces: {…} }

I know its supposed to show what I passed but at some point it just stop showing what I passed in and just started showing controller and the action.

How am I submitting the form on the frontend?

listForm.addEventListener('submit',(event) =>{
     event.preventDefault();   
     
     const property = {
       address: document.getElementById('address').value,
       state: document.getElementById('state').value,
       sale_price: document.getElementById('sale_price').value,
       owner_id: document.getElementById('owner_id').value,
     }

     const listObj = {
      method: 'POST',
      header: {
        'Content-Type': 'application/json',
          "Accept": "application/json"
      },
      body: JSON.stringify(property)
     }
      fetch(PROPERTIES_URL, listObj)
      .then(res => res.json())
      .then((list_data) => {
        let new_listing = renderListing(list_data)
        listings.append(new_listing)
        listForm.reset()
        console.log(list_data)
      })
  })

How can I clear this hurdle over my strong params issue?

1

1 Answers

0
votes

The error you are getting is a result of how strong params works in Rails.

params.require(:property).permit(:address, :state, :sale_price, :owner_id)

The above line is telling Rails you would like to receive your params in this form:

{
  property: {
    address: "123 city st",
    state: "NSW",
    sale_price: 100.00,
    owner_id: 1
  }
}

Notice the require(:property) part tells Rails that the params you want to receive needs to have the key "property". Inside of the property key you are telling it to permit the rest of the keys (:address, :state, :sale_price, :owner_id).

Easiest solution to fix your code is to just change the body your sending through from frontend to match the required format by changing this line:

// Before
body: JSON.stringify(property)
// After
body: JSON.stringify({ property })

You can read more about strong parameters here.