Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby

Rails strong params question

Hi all,

Im having some troubles with a project I'm working on. Be warned i consider myself very much a beginner/novice at all this still :)

To keep things short and sweet, Im using rails & active admin to build up an admin interface where i can perform CRUD operations on my database models, which is all working great. However i recently decided i wanted to add another field to one of my models, a "description" field, so generated a migration, ran db:migrate and updated my list of allowed params in my controller.

My problem is i cannot actually save any data for this new "description" field - wether its via creating a new entry or updating an existing one. I suspect this has something to do with the data being filtered out by strong params (at least thats what seems to be the culprit from what googling/research I've done). However i am under the impression i have set up my strong params correctly, so I'm unsure what else might be causing this issue. Any help is greatly appreciated!!

Using rails 5.1.0 & will post code for my controller/model below.

class CellsController < InheritedResources::Base

  def index
  end

  private

    def cell_params
      params.require(:cell).permit(:name, :description)
    end

end
#app/models/cell.rb
class Cell < ApplicationRecord
    searchkick text_start: [:name], callbacks: :async, suggest: [:name]

    validates :name, presence: true, uniqueness: true

    has_many :cell_markers, dependent: :destroy
    has_many :markers, through: :cell_markers

    accepts_nested_attributes_for :cell_markers, :markers

    after_commit :reindex_Cell

    def reindex_Cell
        Cell.reindex
    end

    def search_data 
        {
            name: name,
            markers: markers.name,
            marker: cell_markers.map(&:marker_id)
        }
    end

end
#database schema for my cell model
  create_table "cells", force: :cascade do |t|
    t.string "name"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.text "Description"
  end

... and finally this is part of active admin, so have permitted the param there too.

ActiveAdmin.register Cell do
# See permitted parameters documentation:
# https://github.com/activeadmin/activeadmin/blob/master/docs/2-resource-customization.md#setting-up-strong-parameters
#
 permit_params :name, :description
#
# or
#
# permit_params do
#   permitted = [:permitted, :attributes]
#   permitted << :other if params[:action] == 'create' && current_user.admin?
#   permitted
# end

end

Again, greatly appreciate any help as I'm sure I've overlooked something :)

Cheers

1 Answer

Found the answer, It was an issue with the naming of my "Description" column, as its not meant to have a capitalised "D".

So created a new migration that looked like this:

class Change < ActiveRecord::Migration[5.1]
  def change
    rename_column :cells, :Description, :description
  end
end

and left permit params methods in my controller and active admin resource unchanged.

Hope this can help anyone with similar issues. :)