Ruby on Rails Tuesday, February 9, 2016

Property has many lists. List has many properties. Users can add properties to a list , here user must be owner of those properties which he want to add.

Here is my code:

class List < ActiveRecord::Base    has_many :propertyships    has_many :properties, :through => :propertyships  end    class Propertyship < ActiveRecord::Base    belongs_to :list    belongs_to :property  end    class Property < ActiveRecord::Base    has_many :propertyships    has_many :lists, :through => :propertyships  end

in view:

<%= simple_form_for(@list, url: add_property_list_path,  :method => :put) do |f| %>     <%= f.collection_check_boxes(:property_ids, @user_all_properties, :id, :street_address) %>     <%= f.submit "Add Property" %>  <% end %>

in routes:

  resources :lists do      member do        put 'add_property'      end    end

in controller:

  def add_property      @list = List.find(params[:id])      @list.update_attributes(list_params)    end    private    def list_params      params.require(:list).permit(:name, :user_id, property_ids: [])    end

when I checked properties and I click "Add property" button as other user , its add checked properties but remove old properties that I have in that particular list. what a I doing wrong??


--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/024e7691-f1b3-457f-9794-852cd1a36c78%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment