Rails - 将表单提交给控制器操作

时间:2015-02-11 13:43:57

标签: ruby-on-rails ajax routes

我有ProjectsController,我在那里添加了一个名为search_for_members的方法我希望这个方法在用户点击通过ajax提交时用成员名称更新div这里的问题是我收到路由错误

  

没有路线匹配[POST]“/ projects / 47”

任何帮助?

的routes.rb

  resources :projects do
     resources :user_stories
     get '/projects/:project_id/searchformembers' => 'projects#searchformembers'
  end

视图/项目/显示

      <%= form_tag :url => {:action => "search_for_members"} , :method => 'get', :remote => true do%>
         <div>
          <%= text_field_tag :search, params[:search], :class => 'form-control', :placeholder => 'User name'%>
        </div>
        <%= submit_tag "Add user", :name => nil, :class => "btn btn-info"%>
      <% end%>

      <div id="members">
        <%= render @users %> 
      <% end %>

projects_controller

 def search_for_members
   if (params[:search])
     @users = User.search(params[:search],   @project).page(params[:page]).per_page(4) 
  else 
     @users = @project.users
  end    
  respond_to do |format|
   format.js
  end 
 end

1 个答案:

答案 0 :(得分:1)

您的路线定义错误,与您的控制器或form_for用法不匹配。像这样改变你的路线:

resources :projects do
  get :search_for_members, :on => :member
end

然后在您的视图中更新您的form_for定义,如下所示:

  <%= form_tag search_for_members_project_path(@project), :method => 'get', :remote => true do%>

你应该全力以赴。