Rails 3 - check box to create (opposite _destroy)

I have a query model with a has_many relation to OutputFields. In my new request controller function, I create several instances of OutputField inside the request instance. In my form, I want each flag to determine whether the object is saved (checking means storing this instance of OutputField in the database). How can i do this?

my models:

class Query < ActiveRecord::Base attr_accessible :description, :name has_many :output_fields, :dependent => :destroy accepts_nested_attributes_for :output_fields end class OutputField < ActiveRecord::Base attr_accessible :query_id, :column_name, :table_name belongs_to :query end 

relevant sections of my query controller. Structure is another model.

  # GET /queries/new # GET /queries/new.json def new @query = Query.new Structure.columns.each do |column| @query.output_fields.build( :table_name => Structure.table_name, :column_name => column.name ) end respond_to do |format| format.html # new.html.erb format.json { render :json => @query } end end 

Finally, my opinion. Right now, I'm binding a checkbox to the destroy attribute, which I think will do the exact same thing I want.

 <%= form_for(@query) do |f| %> <%= f.fields_for :output_fields do |builder| %> <div class="field"> <%= builder.check_box :_destroy %> <%= builder.label :_destroy, builder.object.column_name %> </div> <% end %> <div class="actions"> <%= f.submit %> </div> <% end %> 

If this is not obvious, I am trying to create a user interface for a simple query builder. This is my first rails app, so any advice is appreciated.

+6
source share
1 answer

By default, the value of the check_box form helper is to set the checked_value value to '1', and the unchecked_value value to '0'. Therefore, to change the behavior of the destroy flag, simply toggle these values.

 <%= builder.check_box :_destroy, {}, '0', '1' %> 
+9
source

Source: https://habr.com/ru/post/944224/


All Articles