As I’m new to rails and webapps I don’t really know the jargon of how to ask this so I’ll just describe what I’d like to achieve
In my controller I have, for instance, display of all messages from users all around the world. I’d like the user to be able to decide whether the messages will be from all around the world or from his country.
I know how to get the relevant messages and put them in the relevant variables:
@msg_glbl = msgs.all
@msg_lcl = msgs.find_all_by_local(true)
My problem is in the logic and flow of how to make this work. I’d like the web page to have a link/button/image of a flag so when the user clicks on it the controller will do something.
I know I need Javascript for this but I don’t know how to make it so the Javascript will call a specific method in the controller or some other action.
Something like:
javascript: onclick(flag) do something in controller
the controller will place in @msgs the appropriate variable (global or local) and the views will always render @msgs.
I realize I am missing something basic I just don’t know how to search for this in Google.
In rails you can create GET actions that will be accessible by URL and you can add URL parameters accesible in the action scope.
Lets say your controller is called pages and your model Message. You would then have 1 action in your controller (app/controller/pages_controller.rb) that can be called in two different ways:
Only one view in app/views/pages/messages.html.erb
1 routes in config/routes.rb
Your links would point to /pages/messages and /pages/messages?only_country=1
Cheers.
EDITED: Changed everything to pages_controller.rb