I have a few buttons on my index page that are not in a form and that act as filters for markers on a map.
I am trying to post the filter choices as parameters to my index action.
How do I do something like this?
function filterchanged()
{
$.post('/index.html.erb', { filter1: 'value1', filter2: 'false' }, function(result) {
alert('successfully posted filter1=value1&key2=filter2 to index.html.erb');
});
....
}
My index controller action looks something like this:
def index
if params.has_key?(:filter1)
if params[:filter2]
@posts = Post.typeA
else
@posts = Post.typeB
end
else
@posts = Post.all
end
respond_to do |format|
format.html # index.html.erb
format.json { render json: @posts }
end
end
Is this the correct approach?
Try adding 2 hidden fields to the form with names equal to filter1 and filter2 (so the form passes them to your controller action. Use an onClick event for each button to update the respective hidden field values, toggling the values if need be. You may default the hidden fields to 0 or false or whatever you'd like.
Then, in your controller action check for the value of the filter variables.
Your path for index action is really weird. /index.html.erb
is the file name of your index view and is not intended to be used in this way.
To point to that action you must use route paths.
Open a terminal and run rake routes
and check the correct route for your index action. All the routes are setted in /config/routes.rb
I think you should take a look to this: http://guides.rubyonrails.org/routing.html
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.