Reading and doing some RoR tutorials, they say that we should have a private method to request an object and indicate its properties. For example:
def category_params
params.require(:category).permit(:name)
end
When I create a new category (the data in this example), I use in my create action:
@category = Category.new(category_params)
But, when I update a category, I can't do @category = Category.new(category_params)
because the :category
doesn't return in the params
. To this situation, I saw in the comentaries of this question that the require(:category)
need to be removed to the update action work. That fix the problem, but in other way, when I create a new category using the category_params
so, always an empty object is saved.
Have two private methods, onde with require
and other without isn't a good practice, or is? What is the good practice in this case?
EDIT 1 (my update and edit action):
def edit
@category = Category.find(params[:id])
end
def update
@category = Category.find(params[:id])
if @category.update_attributes(category_params)
# TO DO
else
render 'edit'
end
end