Reputation: 1342
URL : /evaluations
I have made a form to select a specific item (a period)
class EvaluationsController < ApplicationController
def index
@periods = Period.all
end
My form :
<% form_for XXXXX do %>
<%= collection_select(:period, :period_id, @periods, :id, :fullname) %>
<%= submit_tag("Valider") %>
<% end %>
I would like the form to go to /evaluations/3 when submited (if the selected period is 3).
When I go manually to /evaluations/3 it works like a charm but I really don't know how to write the form_for to go the right url by submitting the form.
Upvotes: 0
Views: 871
Reputation: 126
Simple way
Submit period ID to process data, and then redirect to action, which handles
evaluations/:id
with :id
as parameters.
redirect_to <youraction>(:period => @id)
This should do the trick.
Not so simple way
If you want to change something dynamically on your page after data was submitted - call method and respond with javascript
respond_to do |format|
format.js
end
In javascript response you can put whatever you want - simple redirects or script, which will change page dynamically. It's up to you.
Hope it helps.
Upvotes: 1
Reputation: 29599
you need to use some javascript to update the action
of the form
$('#period_period_id').change(function() {
$('form').attr('action', '/evaluations/' + this.value);
})
Upvotes: 0