5

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.

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

3 Comments

I understood now why that code was not working. it is like this -> "<form accept-charset="UTF-8" action="/deployment_group/show_workflow_list" data-remote="true" method="get"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="&#x2713;" /></div>" and "<select id="folder_name_foldernames" name="folder_name[foldernames]">" for the above rails code. Now please let me know if i what id should i write in the jqeury code
Hmmm, ok. You must to add :html => { :id => "show_workflow_list_form" } in the form_for method and change $('#foldernames') by $('#folder_name_foldernames') in the js file.
I added a new question for this. Please see it here im not able to see any change for the code when i add it in application.js or even in deployment_group.js.coffee
1

It is because you are using the jQuery submit call incorrectly

Check this article...it worked for me Rails remote form submit through javascript

Comments

0

try this

<%= f.select :foldernames, options_for_select(@folders, @folders.first), {}, {:onchange => remote_function(:method => :get, :url => {:action => 'show_workflow_list'}, :with => 'Form.Element.serialize(this)')}%><br /><br />

4 Comments

remote_function is deprecated in rails > 3.0 and iam using rails 3.2.6 here is the link saying that it is deprecated
one more thing is that i need to submit the selected value as parameter before calling the the function if write the code i think it will try to execute the code in the action without sending any parameters as im not submitting the form anywhere
in with it is sending all the form parameters value.
you can find the deprecation solution here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.