Skip to content

Conversation

@peterwake
Copy link

Please see the commit comments for details on how to get this working - but please note that I was unable to run the bundle (and hence do testing with appraisal). Also I haven't managed to understand what respond_with_bip really does, so this might need more coding.

I have an ActiveRecord model that references another model, and I
wanted to get Best In Place to create a select dropdown with a list of
areas for the user.
For my scenario, I have a Store that belongs to an Area.

In my model I have:
```ruby
class Store < ActiveRecord::Base
  belongs_to :area
```

So in my view (I use am using haml):
```ruby
= best_in_place @store, :area, :as => :select, :collection => @areas
```

In routes.rb, make sure the stores can be updated:
```ruby
resources :stores, only: [:index, :show, :update] do (etc)
```

In stores_controller.rb:
```ruby
  def details
    @store = Store.find(params[:id])
    @areas = current_user.visible_areas
    #... etc
  end

  def update
    store = Store.find(params[:id])
    store.assign_attributes(update_params)
    store.save!
    respond_with_bip(store)
  end

  private

  def update_params
    params.require(:store).permit(:id, :area_id) # other fields in here
of course!
  end
```

Please note that the update_params need to be (say) :area_id rather
than :area to work.

Also I haven't managed to understand what respond_with_bip really does,
so this might need more coding.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants