lundi 30 mai 2016

Rails Form Select, "InvalidForeignKey"

The error:

Rails Error Page. ActiveRecord::InvalidForeignKey in Devise::RegistrationsController#destroy

So, I'm new to ruby and rails, looking for a way to implement a simple form for whether or not a user wants to be anonymous. When changing the value it works the first time, but to choose the other option the second time the above error comes up.

I'm working with Devise and have other user options like age..etc., but here is the code:

<%= form_for(resource, as: resource_name, url: registration_path(resource_name), html: { method: :put }) do |f| %>
  <%= devise_error_messages! %>

 ... [OTHER OPTIONS] ... 

     <%= f.select :anonymous, [['Anonymous', true], ['Yourself', false]] %>

I have anonymous permitted:

  def configure_permitted_parameters
    devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:name, :email, :password) }
    devise_parameter_sanitizer.for(:account_update) { |u| u.permit(:name, :email, :password, :current_password, [other options], :anonymous) }
  end

The migration:

class AddAnonymousToUsers < ActiveRecord::Migration[5.0]
  def change
    add_column :users, :anonymous, :boolean
  end
end

My goal is to be able to call on a user like so; user.anonymous so that I can handle it accordingly. I'm open to other options/insight as well. Not sure what the delete has to do with any of this.




Aucun commentaire:

Enregistrer un commentaire