Joe Essey
Joe Essey

Reputation: 3527

404 on AJAX get request in rails app

The button that makes the request:

<%= link_to business_domain.name, data_services_fetch_by_business_domain_path(:business_domain_id => business_domain.id, :data_services => @data_services), :remote => true %>

The route in routes.rb:

get "data_services_fetch_by_business_domain" => 'data_services#fetch_by_business_domain', :as => :data_services_fetch_by_business_domain

Controller method:

  def fetch_by_business_domain
    @data_services_for_domain = BusinessDomain.find(params[:business_domain_id]).data_services
    params[:data_services] & @data_services_for_domain

    respond_to do |format|
      format.js { render :action => display_data_service_records.js.erb }
    end
  end

display_data_service_records.js.erb referenced from controller method:

$("#data-service-listings").html("<%= escape_javascript(render partial: 'listings', locals: { data_services: @data_services } ) %>");

Listings partial that should be updated:

<div class="well" id="data-service-listings">
...
</div>

Is there a mistake above, or is this approach fundamentally wrong? Thanks.

Upvotes: 0

Views: 81

Answers (1)

Mandeep
Mandeep

Reputation: 9173

If you look at the route you have made

get "data_services_fetch_by_business_domain" => 'data_services#fetch_by_business_domain', :as => :data_services_fetch_by_business_domain

You are making a route with a get request but if you want to send some data you should use post request.

Change your url to

post "business_domain/:business_domain_id" => 'data_services#fetch_by_business_domain', :as => :data_services_fetch_by_business_domain

and your link_to would look like:

<%= link_to business_domain.name, data_services_fetch_by_business_domain_path(:business_domain_id => business_domain.id, :data_services => @data_services),method: :post, remote: true %>

You would be able to access data_services array by params[:data_services] and bussiness_domain_id by params[:bussiness_domain_id]

Upvotes: 1

Related Questions