2

I have a controller action like this. itineraries_controller.rb

def delete_city
  @new_itinerary = params[:new_itinerary] if params[:new_itinerary].present?
  @step = params[:step]
  if @step == 'two'
    @city = City.find(params[:id])
    @delete_rec = true
  else  
    @city_item = CityItem.find(params[:id]) rescue nil
  end  
  if @city_item.present?
    @itinerary = @city_item.country_item.itinerary rescue nil
    @itinerary = Itinerary.unscoped.find(params[:data_id])
    if (@itinerary.present? and @itinerary.user_id == current_user.id) or (current_user.moderator?)
      @country_item = @city_item.country_item
      @city_item.destroy
      @country_item.update_column('city_ids', @country_item.city_items.order(:sequence).pluck(:city_id))
      @delete_rec = true
    end
  end
  respond_to do |format|
    if @delete_rec && @step != 'two'
      format.js {render 'step_two.js.erb'}
    elsif @delete_rec && @step == "two"
      format.js {render 'delete_city_step_2.js.erb'}    
    else
      format.js {render :text => 'fail'}
    end
  end
end

And js.erb like this:

delete_city_step_2.js.erb

a = $('a.cityCrossBtn[for='+"<%= @city %>"+']')
li = a.closest('.token-input-token-facebook')
li.hide();

I am not able to find a element. What is the correct syntax?

1 Answer 1

2

@city will give you an instance of ActiveRecord::Base class, and I think it's something you aren't expecting in your .js.erb file. You may be wanting an attribute related to that instance like id or name, and you can accomplish that by writing the following code:

a = $('a.cityCrossBtn[for='+"<%= @city.id  %>"+']')
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.