ruby on rails jquery submit a form as js
jquery, ruby-on-rails, ruby-on-rails-3
Solution
On a JS file you should do this :
$('#foldernames').change(function() {
$('#folder_name').submit()
});
And in your controller you should have this :
def show_workflow_list
//code here
render :format => :js
end
When you change the value, it should submit your form so it should send an ajax request to your controller. Your controller should render a js format and execute your `show_workflow_list.js.erb`. To check it, you can use a javascript console like firebug.
Problem
I have this code in my view: ``` <%= form_for :folder_name, :remote => true, :method => "get", :url => {:action => "show_workflow_list"} do |f| %> <%= f.select :foldernames, options_for_select(@folders, @folders.first), {}, {:onchange => ("this.form.submit()")}%><br /><br /> <%= hidden_field_tag 'selected_domain', params[:domain_selected] %> ``` When I change the value in the dropdown, the form is being submitted as: ``` Processing by DeploymentGroupController#show_workflow_list as HTML Parameters: {"utf8"=>"Γ£ô", "folder_name"=>{"foldernames"=>"DETAIL_ADJUSTMENT" ``` When I add a submit button instead of a `:onchange=>` like below: ``` <%= form_for :folder_name, :remote => true, :method => "get", :url => {:action => "show_workflow_list"} do |f| %> <%= f.select :foldernames, options_for_select(@folders, @folders.first)%><br /><br /> <%= hidden_field_tag 'selected_domain', params[:domain_selected] %> <pre><%= f.submit "Submit"%></pre> ``` the request is being submitted like this: ``` Processing by DeploymentGroupController#show_workflow_list as JS Parameters: {"utf8"=>"Γ£ô", "folder_name"=>{"foldernames"=>"DETAIL_ADJUSTMENT" ``` I have the following code in my `show_workflow_list` action: ``` def show_workflow_list //some code respond_to do |format| format.js end end ``` and I have a `show_workflow_list.js.erb` file which has the following content: ``` $('#workflow_selection').html("<%=j render "show_workflow_list" %>"); ``` The problem is when I change it to `onchange=> submit`, it is processing the action as HTML: ``` Processing by DeploymentGroupController#show_workflow_list as HTML ``` and not as JS when there is a submit button: ``` Processing by DeploymentGroupController#show_workflow_list as JS ``` so I'm getting back a 406 status error and the `show_workflow_list` is not being rendered. Update: I understood why `onchange=>` is being sent as HTML. The reason is that the format for select is "`select(object, method, choices, options = {}, html_options = {})`". The place i'm mentioning `":onchange=>this.for.submit" is under html_options={}"` which is why it is being submitted as HTML. I need to render a partial `_show_workflow_list.html.erb` between a `<div>` on the same page, so I changed the code in my controller like this: ``` def show_workflow_list //code here respond_to do |format| format.html{?} end end ``` In the code above, I need to fill something in `{}` the `format.html` so that it will render the partial `_show_workflow_list` between the `div` tags in my `make_deployment_group.html.erb`.