Rails throwing Unpermitted parameters: name error when using Devise - ruby-on-rails

I am trying to follow this as an example: https://github.com/RailsApps/rails-devise/
I made a project of my own where I try it out and everything is working fine exept for the name part. If I try to make a new user or edit it, rails will throw this Unpermitted parameters: name error.
I have added name as a string into the users table and everything should be fine. I read that some people suggested adding a specific user_params mathod to allow :name but the example I linked before does not have it and in there it all works.
Also worth noting maybe that a direct INSERT INTO in pgAdmin will insert a new row with a name without any problems. So it's something with Rails.

"Unpermitted parameters" means you're doing mass assignment without marking the passed in parameter as permitted. Have another look at this section of the example app documentation: https://github.com/RailsApps/rails-devise/#adding-a-name-attribute. Make sure the name attribute is listed in config/initializers/devise_permitted_parameters.rb.

Related

Sending form fields not related to passed model: Can't mass-assign protected attributes

I am using a form_for loop to submit data to my controller’s create method. In the form_for I have several text_fields that are not associated to any field on the table. They are essentially free text fields that I want to put data in and use for additional logic in the create method.
When I submit the form, the first line of the create is hit:
#user = User.new(params[:user])
The following error occurs (:test1 being my form field unrelated to the model):
Can't mass-assign protected attributes: test1
I realize it is because of the text fields I’m sending that are not related to the model. I have tried all sorts of strange syntaxes such as:
User.new(params.require(:user).except(:test1)) and User.new(params[:user].except(:test1)) to no avail.
I’ve found several sources online stating you can use the except or delete method but I can’t seem to get it to work, especially while in the User.new().
I have tried it outside the new as well:
params.delete(:test1)
#user = User.new(params[:xref_ability])
I also want to be able to read the additional attributes eventually in the create, so I don’t think the except() or delete() is going to solve all my issues (if it did work). Therefore, I tried attr_accessor :test1 on the controller class and the model class hoping it would give the method permission to read the attribute, but no success.
I've also tried adding it to attr_accessible on my model which then leads to the error:
unknown attribute: test1
Any other suggestions?
Maybe try using virtual attributes? It makes sense to use them if they don't map to a field in the DB.
RB has a good railscast on the topic: http://railscasts.com/episodes/16-virtual-attributes-revised
You probably need a attr_accessible :test1 on your User model, give it a try.
You are getting the unknown attribute error because it doesn't exist in the database.
You must create test1 in database (add it with a migration...) or just add it to your model with:
attr_accessor :test1
This way you'll have the value in the object but it won't store in the database. If you need to store it, add it to the database and use attr_accessible as #Miguelgraz told you before.

Model Accepts Nesting Attributes without declaring that it accepts them

everyone. So, I'm working on a basic Rails 4 application for practice, and I have a model for FriendCircle and a model for FriendCircleMembership. (the FriendCircleMembership's corresponding table is basically a join table).
In the console, I'm able to create a new FriendCircle object while passing in :friend_circle_memberships_attributes. This successfully inserts a new FriendCircle row into my table as well as inserting the proper rows into the FriendCircleMembership table.
The WEIRD thing is that, even if i comment out that the FriendCircle accepts_nested_attributes for :friend_circle_memberships, it still works. Is this because i am whitelisting it as a permission in the controller?
The other issue is that, even though i can successfully make the nested objects via the rails console, when i try making it through my html form it says my friend_circle_memberships_attributes is an unpermitted parameter. Not sure why this is happening. I check the incoming parameters and they look fine.
any help would be SWEEEEET. thanks.
I determined what the error was: One of my controllers for a nested attributes was validating the presence of the id of the controller it was nested under.
I'm assuming the validation occurs before an id is created, which makes sense, but im not 100%. So i just took out the validator and things worked.

composite_primary_keys - How to pass both from view to controller?

I have installed composite_primary_keys, and set my two primary keys. My delete method works, as it doesn't need to pass any id's. However I am unsure how to get my edit path to function.
If I leave it including only the default :id, I get this message (as expected)
Incorrect number of primary keys for Deal: ["id", "merchant_id"]
I have found that for the edit form to render, I effectively have to make the URL look this this
http://localhost:3000/deals/2,1/edit
My attempts to get both values into the edit_deals_path() have not worked.
Is there a tutorial somewhere on how to use composite_primary_keys gem? The author's website doesn't include this issue.

Legacy table having problems with set_primary_key

I generated a scaffold against a legacy SQL Server database view that has a text primary key with no id column. I have set_primary_key "Gift_ID" in the model but Im getting an error on the show method.
I can list the records okay but when I click on the show link, it errors out. It appears to be using id to look up the record even though I have set_primary_key in the model. Here is the complete text of the error displayed;
NoMethodError in PledgesController#show
undefined method `eq' for nil:NilClass
Rails.root: /home/steve/RubymineProjects/otatest
Application Trace | Framework Trace | Full Trace
app/controllers/pledges_controller.rb:16:in `show'
Request
Parameters:
{"id"=>"PL-24681"}
Show session dump
Show env dump
Response
Headers:
None
Can anyone shed some light on why it is still trying to use "id" as the key?
Thanks for any help!
I solved my problem, so I thought I'd answer my own question in case it helps someone else;
I gave up on using the set_primary_key option in the model and added an "id" field to the view in my legacy system (SQL Server) using a key field in the data. That solved the id problem.
I also found that the generated scaffolding field names are case sensitive. So I used the view to set all field names to lowercase. You could make sure your scaffolding case matches the legacy field names, but I found that it's easier to just set them all to lower case.
One more thing; I had a few legacy boolean fields that had a "?" on the end of the field name, the scaffolding did not like that either, so I removed the "?" in the view.
Bottom line, building a Rails app on top of a legacy system has it's challenges. I discovered most my problems by migrating the table to the legacy database and examining the way it created the fields then modifying my view to match.

mongoid atomic updates in rails

i am having an issue with updating existing documents composite key fields in rails. the associated mongo query for my update_attributes statement seems to be correct, however the object cannot be found afterwards.
for example with an existing object with first_name "Jane" and last_name "Doe"... with my :key being :first_name, :last_name
i hit my update method with:
{"artist"=>{"last_name"=>"Doe", "first_name"=>"John"}, "commit"=>"Update Artist", "id"=>"jane-doe"}
def update
#artist = Artist.find(params[:id])
if #artist.update_attributes(params[:artist])
redirect_to #artist
end
end
which generates the mongo query: MONGODB app_database['artists'].update({"_id"=>"jane-doe"}, {"$set"=>{"_id"=>"john-doe", "first_name"=>"John"}})
which seems correct to me... but when i am redirected to that new artist, it complains about Document not found for class Artist with id(s) john-doe.
and in fact looking at my db from the mongo console, i still see Jane Doe in there. It is something to do with them being composite key fields, since i can update non-key fields just fine.
what am i missing here?
I tried this in an app of my own, and it looks like MongoDB simply doesn't currently allow you to modify the _id field as part of a $set operation (which is what Mongoid uses to perform updates). This is a strange restriction - I've been using Mongo for a year and a half now and I've never run into it.
So, a few options:
Stop using anything for your _id that might need to be changed later. I'd do this - it's a best practice anyway.
Whenever you need to make one of these _id changes, instead create a new record with the new _id attribute and delete the old one. This might get messy though, especially if you have other models that refer your Artist models by id.
File an issue with 10gen asking for this restriction to be lifted. They're very good about responding to users' concerns, but even if they agree, it'll probably take a while to be done.
File an issue with Mongoid to request support for these types of changes (Mongoid could conceivably handle the create + delete mechanism for you), but honestly, it's the kind of edge case that's probably not worth the extra time and code for them to support. It'd be nice if Mongoid raised an error when you tried to do an update like this, at the very least.

Resources