开发者

How do I make a button that resorts data on a page?

开发者 https://www.devze.com 2023-04-12 00:51 出处:网络
I have a page that lists all the events in my database of events. I want to create a button that says \"my events\" that when pressed reloads the page but then only displays events that belong to the

I have a page that lists all the events in my database of events. I want to create a button that says "my events" that when pressed reloads the page but then only displays events that belong to the current_user. I tried d开发者_如何学编程oing something like this:

<%= button_to "My Events", events_path(:my_events => true) %>

and then I was going to add an if statement to the events_controller index that filters @events if my_events is true. However, this button just creates a new event for some reason.

Is there a better way of doing this?


this will do what you want:
add an action find_my_events in your events controller:
find_my_events
@events = current_user.events(:all)
render :action => 'index'
end

Now, in your view, create a form_tag and associate it with find_my_events:
(you can either create a new route in your routes.rb or use :controller => 'events', :action => 'find_my_events', i prefer the former)
Add a submit_tag to your form.

So, now when you submit the form, the page refreshes and fetches events for the current user.

You can also submit a form to your index action with a boolean hidden field. Now in your index action check if that boolean field is set to true, if yes, display events for the current_user or all events otherwise.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号