Rails 3 - Invoke controller method with submit_tag -


i'm new rails, please excuse me if i'm asking such basic question.

i had form on html.erb page:

<%= form_tag(posts_path(:controller => "posts", :action => "create_thread"), :method => "post") do%>    title:     <br></br>    <%= text_area_tag 'title', 'thread\'s title required!', :rows => 1, :cols => 30 %>    <br></br>    message:     <br></br>    <%= text_area_tag 'body', nil, :rows => 15, :cols => 50 %>    <br></br>    <%= submit_tag "create thread" %> <% end %> 

i defined "create_thread" method in controller:

class postscontroller < applicationcontroller     def create_thread       logger.info("thread created: ")     end end 

in routes.rb file, created route submit:

resources :posts collection   post 'index', :as => :create_thread end 

basically, when click on "create thread" button on form, rails execute function "create_thread" in postscontroller class, , load "index" page.

however, when clicked on "create thread" button, took me straight "index" page (so path working, @ least), did not execute "create_thread" function in controller.

this showed on console when clicked on button:

started post "/posts?action=create_thread" 127.0.0.1 @ 2013-07-14 22:08:35 -0700 processing postscontroller#index html parameters: {"utf8"=>"✓", "authenticity_token"=>"9xvurstaysmdoc6ug/a3xxx/8bzlky8ixckiafhs9fu=", "title"=>"thread's title required!", "body"=>"", "commit"=>"create thread"} 

here's output of rake routes

               root        /                                   posts#index    new_thread_posts    /posts/new_thread(.:format)         posts#new_thread create_thread_posts post   /posts(.:format)                    posts#index current_thread_post    /posts/:id/current_thread(.:format) posts#current_thread               posts    /posts(.:format)                    posts#index                     post   /posts(.:format)                    posts#create            new_post    /posts/new(.:format)                posts#new           edit_post    /posts/:id/edit(.:format)           posts#edit                post    /posts/:id(.:format)                posts#show                     put    /posts/:id(.:format)                posts#update                     delete /posts/:id(.:format)                posts#destroy 

so, how rails execute "create_thread" function in postscontroller? have been searching on web in past 2 days, , trying sorts of stuff, none has worked me.

any hint or pointer appreciated!

thank in advance help.

try following

resources :posts   post :create_thread, on: :collection end  form_tag(controller: :posts, action: :create_thread) 

Comments