-1
目前,我有一个list_companies#index下拉列表的公司列表。用户当前必须做的是选择任何一家公司下拉并单击选择项目按钮。然后,当点击提交按钮时,它会将该公司的ID传递到会话中。使用集合下拉选择传递对象的id到rails中的会话4
我的问题是 - 如何将下拉选定公司的ID传递到会话中。
list_companies /索引:
<h3>Select Company</h3>
<%= simple_form_for :Select_Company, :url => {action: 'select_company'}, :method => 'post' do |f| %>
<label for="company_name">Company Name</label> <%=render 'company_dropdown'%>
<%= submit_tag "Select Company", :type => 'button' ,:onclick=>"getNewPage()"%>
<%end%>
<script>
function getNewPage()
{
var form = "";
var compId = document.getElementById("company_company_id").value;
if (!compId)
{
alert("Please select a Company");
}else
{
form = document.forms[0];
form.method = "post";
form.action = "list_companies/select_company";
form.submit();
}
}
</script>
_company_dropdown.html.erb
<% if @list_companies != nil then%>
<%= collection_select(:company, :company_id, @list_companies, :company_id, :company_name,{:selected=>@company_id,:prompt=> "Select Company"}) %>
<%else%>
<select >
<option value="">Select Company</option>
</select>
<%end%>
list_companies_controller.rb
class ListCompaniesController < ApplicationController
load_and_authorize_resource
def index
@list_companies=Company.all
respond_to do |format|
format.html
format.js
end
end
def select_company
company = params[:company]
puts "current_dashboard_user.company_id ===>" +current_dashboard_user.company_id.to_s
if (company != nil) then
current_dashboard_user.company_id = company[:company_id]
session[:current_dashboard_user] = current_dashboard_user.id
session[:company_id] = company[:company_id]
respond_to do |format|
format.html {redirect_to root_url}
end
else
format.html { render :index}
end
end
end
的routes.rb
resources :list_companies do
collection do
get :select_company
post :select_company
end
end
现在company_id没有存储在会话中。
当我点击提交按钮时,什么都不会发生。 @Santosh – 2014-12-03 08:44:22
您将需要定义一个ajax目标,它将接收您的id并将其分配给您的会话变量... – 2014-12-03 09:39:36