mardi 3 mars 2015

Refactoring API controller with respond_with and responders gem

I was doing an API and started using render, in this way:



def create
begin
alert_for_filter = AlertForFilter.new
alert_for_filter.user = current_user
alert_for_filter.name = params[:alert_for_filter][:name]
alert_for_filter.category = Categoria.find(params[:alert_for_filter][:category_id])
alert_for_filter.filters = create_filters(params[:alert_for_filter][:filters])
alert_for_filter.save!

render json: alert_for_filter, only: alert_for_filter_permited_fields, status: 200
rescue
render json: "Errors", status: 400
end
end


But now I want to refactor this using respond_with and responders gem.


I tried to remove the "begin-rescue", the save! and render. And added that:



respond_with(alert_for_filter)


How can I refactor this?


Aucun commentaire:

Enregistrer un commentaire