jagass
jagass

Reputation: 11

How to call an action without a route with ajax and rails

I have a view containing a form that is supposed to call an action (home#search) via ajax. The action does not have a route because i want to prevent people from making queries by typing in the url (e. g. "www.example.com/home/search?q=blablabla") in their browser. The action should be restricted to the ajax request coming from the rails application itself.

<%= form_tag("???", method: "get", remote: true) do %>
  <%= label_tag(:q, "Type in location:") %>
  <%= text_field_tag(:q) %>
  <%= submit_tag("Search") %>
<% end %>

So how can you call an action via ajax in rails that doesn't have a route? Or is there any other solution to this?

Thank in advance! :)

Upvotes: 0

Views: 1496

Answers (3)

mccannf
mccannf

Reputation: 16659

You will need a route defined. But you can also supply a temporary access token for your AJAX call to use:

http://railscasts.com/episodes/352-securing-an-api?view=asciicast

Upvotes: 0

maximus ツ
maximus ツ

Reputation: 8065

You can use something like this,

 def new
  @user = User.new
   respond_to do |format|
    format.html { render text: "Error", status: 404 }
    format.js
   end
 end

Upvotes: 1

Rebitzele
Rebitzele

Reputation: 3282

An ajax call definitely needs a route. However, you can make a POST request rather than a GET request, and this way a user can't enter it into their browser. However, they still could technically make such a request using clients like CURL.

Upvotes: 0

Related Questions