I’m using Rails 4.2.3. I have this in my controller …
def create
@myobject = MyObject.new(MyObject_params(:params))
@current_user = User.find(session["user_id"])
@myobject.user = @current_user
if @myobject.save
respond_to do |format|
format.html { redirect_to controller: "users", action: "index", notice: 'Saved successfully.' }
end
else
format.html { render action: "index" }
end
end
private
def MyObject_params(params)
# Main Code goes here
params.require(:myobject).permit(:time_in_ms, :name)
params[:myobject][:time_in_ms] = (params[:myobject][:hour].to_i * 60 * 60 + params[:myobject][:minute].to_i * 60 + params[:myobject][:second].to_i) * 1000
end
but when I submit my form, I get the error
private method `require' called for :params:Symbol
on this line, “params.require(:myobject).permit(:time_in_ms, :name, :distance, :distance_units)”. Not sure what I’m missing (or I have accidentally included). Thanks for help, - Dave
Edit:
After trying the suggestion given, I get the error
When assigning attributes, you must pass a hash as an argument.
on the line
@myobject = MyObject.new(MyObject_params)
In addition to making the adjustment in the suggestion, I also changed the method signature, "def MyObject_params(params)" to "def MyObject_params."
As Brad Werth stated you are trying to pass :params into your MyObject_params method. Since you have marked this method as private in your controller, it will refuse to accept an explicit receiver! There is a wealth of information on private vs public vs protected ruby methods but the long short of it is that you can not call any private method directly on an object (such as @user.make_admin
) . This protects you from outside sources trying to manipulate the use of certain methods.
Changing @myobject = MyObject.new(MyObject_params(:params))
to
@myobject = MyObject.new(MyObject_params)
is what you want. I would also recommend following Ruby best practices and downcasing your MyObject_params method to my_object_params
The error message you have is telling you that you are trying to call a private method on a symbol. When you are doing this: @myobject = MyObject.new(MyObject_params(:params))
, you are passing the symbol :params
to the method. It looks like you really want the params hash, which would be @myobject = MyObject.new(MyObject_params(params))
(not a symbol).
As both methods have the same scope, you may consider not passing the params as an argument at all, but rather just refer to it in the MyObject_params
method the same as you would in the create
method. Also consider adjusting MyObject_params
to at least my_object_params
, to fit Ruby conventions, if not something more descriptive.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With