X-editable checklist which creates associations with another model
stopachka opened this issue · comments
This gem has been amazing. I'm using x-editable to implement an asynchronous editing form in one of my apps.
In my models I have, CommonApp which has_many Industries.
What I would like to do is to create an x-editable checklist, which will associate the common_app to the industries that the user checks.
So far, I've been able to edit the CommonApp's values, such as cover_letter, like so -
== editable @common_app, :cover_letter, url: user_common_app_path(@user,@common_app), value: @common_app.cover_letter, defaultValue: "Your cover letter hasn't been filled in yet, click here to fill it out!", type: "textarea"
and
def update
@common_app = CommonApp.find(params[:id])
if @common_app.update_attributes(common_app_params)
render json: {success: true}
else
render json: {errors: @common_app.errors}, status: 400
end
end
However, I'm not sure where to begin with creating the checklist. Write now, I wrote -->
== editable @common_app, :industry_ids, url: user_common_app_path(@user,@common_app), type: "checklist"
javascript:
$(function(){
$('.editable').editable({
value: [2, 3],
source: [
{value: 1, text: 'option1'},
{value: 2, text: 'option2'},
{value: 3, text: 'option3'}
]
});
});
But I'm not able to get those values to show up.
What way should I go about this?
Ah, got it. For anyone who has a similar problem, here's the implementation :
== editable @common_app, :city_ids, url: user_common_app_path(@user,@common_app), type: "checklist", value: @common_app.cities.map{|city| city.name}, source: City.all.map{ | city | {value: city.id, text: city.name} }
It would be better to create source and value on CommonApp
model instead in view, but result is the same.
class CommonApp
def cities_values
cities.map{|city| city.name}
end
def cities_sources
self.class.all.map{ | city | {value: city.id, text: city.name} }
end
end
= editable @common_app, :city_ids, url: user_common_app_path(@user,@common_app), type: "checklist", value: @common_app.cities_values, source: @common_app.cities_sources