目前,我在项目/索引视图中有一个项目列表。当您单击项目旁边的“显示”时,它会转到该项目的显示视图。在那里,您可以单击“选择项目”,这会将那个项目的 id 传递到会话中,这样只有与该项目相关的相关任务才会显示在其他页面中。
我想要做的是为项目提供一个下拉菜单(这样您就不会只看到所有项目的巨大长表),并且在下拉菜单旁边有两个按钮 - 一个用于查看更多信息(即转到该项目的显示视图),然后选择“选择项目”。我怎样才能让他们拥有正确的信息(即项目ID),以便他们正常运行?谢谢!
以下是所有相关代码:
项目下拉列表:
<%= collection_select(:project, :id, Project.all, :id, :name) %>
链接显示视图:
<%= link_to 'Show', project, :class =>"btn btn-primary btn-mini" %>
链接到“选择项目”
<%= link_to 'Select Project', select_project_project_path(@project), :method => :post, :class => 'btn btn-primary btn-small' %>
在项目控制器中选择项目操作:
def select_project
project = Project.find(params[:id])
session[:project_id] = project.id
redirect_to root_url, notice: "Current project set to: #{project.name}, ID: #{project.id}"
end
更新:
以下是相关路线:
resources :projects do
member do
post :select_project
end
end
以及 rake routes|grep 项目的结果:
select_project_project POST /projects/:id/select_project(.:format) projects#select_proj
projects GET /projects(.:format) projects#index
POST /projects(.:format) projects#create
new_project GET /projects/new(.:format) projects#new
edit_project GET /projects/:id/edit(.:format) projects#edit
project GET /projects/:id(.:format) projects#show
PUT /projects/:id(.:format) projects#update
DELETE /projects/:id(.:format) projects#destroy