Question

I need to implement a form with a custom field (which doesn't have it's reference in db) using a checkbox (boolean field).

if the checkbox is checked, the backend must write a certain many-to-many row and point it to the updated object.

The problem is my setter method in article.rb is not being accessed on form post.

article.rb

class Article < ActiveRecord::Base
  attr_accessor :highlight

  def highlight
  end

  def highlight=(arg)
    puts "NO ACCESS"
  end
end

_form.html.erb

<%= semantic_form_for [:admin, @article] do |f| %>
  <%= f.input :title, :label => "Title" %>
  <%= f.input :text, :as => :ckeditor %>
  <%= f.input :highlight, :as => :boolean, :label => "Highlight" %>

article.rb(active_admin)

ActiveAdmin.register Article do
  form partial: 'form'
  permit_params do
    permitted = [:title, :text, :highlight]
    permitted
  end
end
Était-ce utile?

La solution

Javkhlan, attr_accessor :highlight is short form for

def highlight
  @hightlight
end

def highlight=(highlight)
  @highlight = highlight
end

So, your code

def highlight
end

def highlight=(arg)
  puts "NO ACCESS"
end

Just rewrite methods generated by your attr_accessor. Try to remove these two methods and you'll be happy

Autres conseils

Remove

attr_accessor :highlight

and check

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top