如何在Ruby On Rails中传递控制器中的参数

时间:2017-01-13 06:17:31

标签: ruby-on-rails

我试图在控制器中传递参数,但它没有返回任何东西。请帮助我如何传递参数。 控制器代码 -

 def print_salary_slip_monthwise
      @month = params[:salary][:month]
      @year = params[:salary][:year]
      @company = params[:salary][:company_id]
      @company_location = params[:salary][:company_location_id]
      @department = params[:salary][:department_id]
      @salaryslips = Salaryslip.where(month:  @month,year: @year.to_s,employee_id: @salary1)

    end 

form.html.erb - 这是我用于更改事件的表单。

<div class="box">
  <div class="box-header">
    <h3 class="box-title">Salary Slip Department Wise</h3>
  </div><!-- /.box-header -->
  <div class="box-body">
<%= bootstrap_form_for(:pdf_salaries, url: { action: 'print_salary_slip_monthwise'},html: {id: 'pdf_salaries'},remote: true ) do |f| %>
     <div class="row">

      <div class="col-sm-2">
      <label>Year</label>
        <div class="field">
          <%= select :salary,:year,['2015','2016','2017','2018','2019','2020','2021','2022','2023','2024','2025','2026','2027'],{label: 'Select Year',include_blank: " Select Year"},{class: 'form-control'} %>
        </div>
      </div>
      <div class="col-sm-2">
      <label>Month</label>
        <div class="field">
          <%= select :salary,:month, ['January','February','March','April','May','June','July','August','September','October','November','December'],{label: 'Select Month',include_blank: " Select Month"}, class: "form-control" %>
         </div>
      </div>

       <div class="col-sm-2">
            <div class="form-group required">
            <div class="input-group">
              <%= f.select :company_id, all_company,{include_blank: "Select Company"},{onchange:"var a={id:$(this).val(), form : 'employee'}; $.get('/employees/collect_company_location',a,function(response){});"}%>
            </div>
            </div>
          </div>

      <div class="col-sm-2">
            <div class="form-group required">
              <div id="company_location">
                <%= render 'employees/company_location_dropdown' %> 
            </div> 
            </div>
          </div>

       <div class="col-sm-3">
            <div class="form-group required">
            <div class="input-group">
              <div id="department">
                <%= render 'employees/department_dropdown' %> 
            </div> 
            </div>
            </div>
        </div>
      </div>
      <div class="row">
       <div class="col-sm-2">
          <div class="actions">
            <%= f.submit "Display Report", class: "btn btn-sm btn-primary",id: "buttonCtc" %>
          </div>
        </div>
        </div>
         <div class="ajax-progress"></div>
       <div id="employee_list_pdf"></div>
</div>
</div>
<%end%>

2 个答案:

答案 0 :(得分:0)

您可以使用表单或使用ajax传递参数。将参数传递到服务器(控制器)后,可以将其检查到服务器日志以查看参数。在rails 4中,您需要使用Strong Parameters将这些参数保存到数据库中。

答案 1 :(得分:0)

将此作为下面字段表格的示例。

<%= simple_form_for @user do |f| %>
  <%= f.input :username %>
  <%= f.input :password %>
  <%= f.button :submit %>
<% end %>

当提交上述表格时。你可以看到rails服务器终端的params

 Parameters: {"user"=>{"username"=>"user1", "password"=>"password"}, "commit"=>"Submit"}

在控制器中访问params,如下所示:

params[:user][:username]  # code in controller

希望这能给你明确的暗示。