submit form action-> index, from layout

I have a select option tag common to all pages,
following is the code which I have put in partial, rendered it in
layout.

<% form_tag :action => "index" do -%>
<%= select_tag( "state" ,
"<option>State 1</option>
<option>State 2</option>
<option>State 3</option>
<option>State 4</option>
<option>Feature Article</option>", :onchange => "this.form.submit()") %>
<% end -%>

On form submit, following is the o/p -

Sachin Kale wrote:

I have a select option tag common to all pages,
following is the code which I have put in partial, rendered it in
layout.

<% form_tag :action => "index" do -%>
<%= select_tag( "state" ,
"<option>State 1</option>
<option>State 2</option>
<option>State 3</option>
<option>State 4</option>
<option>Feature Article</option>", :onchange => "this.form.submit()") %>
<% end -%>

On form submit, following is the o/p -
----
Unknown action

No action responded to create
-----

I need the select option value in the index method, will then select
appropriate state and request the state from database.

I am newbie, please help me out!

Thanks.

Since you are telling that this form is in all your pages, maybe you
forgot to add the controller name supposed to handle this request.
Otherwise, the request is send to the controller related to your current
view while displaying this form.

Try to update your partial with "my_controller_name" changed to the good
one.

<% form_tag :controller => "my_controller_name", :action => "index" do
-%>

Then, in your controller, your state value will be accessible through

def index
  state_value = params[:state]
end

Guillaume Petit

Guillaume Petit wrote:

Sachin Kale wrote:

I have a select option tag common to all pages,
following is the code which I have put in partial, rendered it in
layout.

<% form_tag :action => "index" do -%>
<%= select_tag( "state" ,
"<option>State 1</option>
<option>State 2</option>
<option>State 3</option>
<option>State 4</option>
<option>Feature Article</option>", :onchange => "this.form.submit()") %>
<% end -%>

On form submit, following is the o/p -
----
Unknown action

No action responded to create
-----

I need the select option value in the index method, will then select
appropriate state and request the state from database.

I am newbie, please help me out!

Thanks.

Since you are telling that this form is in all your pages, maybe you
forgot to add the controller name supposed to handle this request.
Otherwise, the request is send to the controller related to your current
view while displaying this form.

Try to update your partial with "my_controller_name" changed to the good
one.

<% form_tag :controller => "my_controller_name", :action => "index" do
-%>

Then, in your controller, your state value will be accessible through

def index
  state_value = params[:state]
end

Guillaume Petit

Thanks Guillaume for your response.

I need to have the form submit to controller which displays the form.

This form is common to all controllers so I dont want any controller
name, when no name is specified it picks the intended controller (i.e
the current page) but does not understands the -> :action = 'index'.

I tried what you suggested by manually writing the controller name, but
the result was same, it does not get the action 'index'.

I even tried :action => controller.action_name , which is the index
action, but it did not worked.

I have to use the workaround -

def updatestate
      state_value = params[:state]
   redirect_to :action => "index"
end

Thanks Again for your help:)

Sachin Kale wrote:

Guillaume Petit wrote:

Sachin Kale wrote:

I have a select option tag common to all pages,
following is the code which I have put in partial, rendered it in
layout.

<% form_tag :action => "index" do -%>
<%= select_tag( "state" ,
"<option>State 1</option>
<option>State 2</option>
<option>State 3</option>
<option>State 4</option>
<option>Feature Article</option>", :onchange => "this.form.submit()") %>
<% end -%>

On form submit, following is the o/p -
----
Unknown action

No action responded to create
-----

I need the select option value in the index method, will then select
appropriate state and request the state from database.

I am newbie, please help me out!

Thanks.

Since you are telling that this form is in all your pages, maybe you
forgot to add the controller name supposed to handle this request.
Otherwise, the request is send to the controller related to your current
view while displaying this form.

Try to update your partial with "my_controller_name" changed to the good
one.

<% form_tag :controller => "my_controller_name", :action => "index" do
-%>

Then, in your controller, your state value will be accessible through

def index
  state_value = params[:state]
end

Guillaume Petit

Thanks Guillaume for your response.

I need to have the form submit to controller which displays the form.

This form is common to all controllers so I dont want any controller
name, when no name is specified it picks the intended controller (i.e
the current page) but does not understands the -> :action = 'index'.

I tried what you suggested by manually writing the controller name, but
the result was same, it does not get the action 'index'.

I even tried :action => controller.action_name , which is the index
action, but it did not worked.

I have to use the workaround -

def updatestate
      state_value = params[:state]
   redirect_to :action => "index"
end

Thanks Again for your help:)

I figured out the solution to my problem of calling form - > action
index from a layout in => railcast#37 - simple search

I was using 'post' method, plus another mistake was appropriate
controller name..

Following will call the index method of the controller that invokes the
layout

<% form_tag controller.controller_path, :method => 'get' do %>
  <%= select_tag("state", "<option>a</option><option>b</option>"
,:onchange => "this.form.submit()") %>
<% end -%>

Thanks For help!

Sachin,
Would you mind embellishing this topic with a little code from your
application. I find it could be a wonderful way to work around having
to use an observe_field.
Thank you,
Kathleen