Rails best_in_place gem while clicking on the labe

2019-04-10 14:57发布

问题:

I am using the best in place gem - which is great. Only thing is that I want the user to be able to click not just the best in place element in order to edit it, but also the label. Lets say I have this best in place element:

<b>Open Hours</b>
<%= best_in_place @provider, :open_hours, :type => :select, :collection => Provider::PROVIDERS_HOURS %>

I want to be able to click the Open Hours in order to open the edit element. Is it possible?

Thanks

回答1:

For single instance of a class you can simply

<b id='edit-open-hours'>Open Hours</b>
<%= best_in_place @provider, :open_hours, type: :select, collection: Provider::PROVIDERS_HOURS, activator: '#edit-open-hours' %>

If you want to do this with a list view you will need to make each id-identifier unique with an ID

<b id='edit-<%= @provider.id %>'>Open Hours</b>
<%= best_in_place @provider, :open_hours, type: :select, collection: Provider::PROVIDERS_HOURS, activator: '#edit-' + @provider.id.to_s %>


回答2:

This was very helpful, but a small correction: you have to prefix the activator DOM element with the relevant symbol (# for id, . for class).

So the second example would be

<%= best_in_place @provider, :open_hours, type: :select, collection: Provider::PROVIDERS_HOURS, activator: '#edit-' + @provider.id.to_s %>