简体   繁体   English

从控制器中的视图传递值

[英]Passing value from view in controller

I am passing a value from view(entered by user) to controller method which in turn fetches data from DB. 我将值(由用户输入)从视图传递给控制器​​方法,该方法又从DB获取数据。 Below is the code in my view 下面是我认为的代码

<%= form_tag({:controller=>"billings", :action=>"tablebill"}, method: :get) do %>
  <div class="center-data">Table Number:
    <div class="tinput"> <%= text_field_tag(:tableno) %></div>
  </div>
  <div class="ebutton">
    <%= submit_tag "Enter" %>
  </div>
<% end %>

Value tableno is what I need in my controller method and is coded as below 值tableno是我的控制器方法中需要的,并且编码如下

def tablebill
  @orders = List
   .select("itemname,tableno,quantity,amount")
   .where(:tableno => params[:tableno]).to_a

  @orders.each do |order|
    @totamount = @totamount + order.amount
  end

  @tax = @totamount/5
  @finalamount = @totamount + @tax

  respond_to do |format|
    format.html # tablebill.html.erb
  end  
end

The value is being passed successfully in the query but its returning nil. 该值已在查询中成功传递,但返回nil。 Below is the snippet from my server 以下是我的服务器中的代码段

Started GET "/billings/tablebill?utf8=%E2%9C%93&tableno=08&commit=Enter" for 127
.0.0.1 at 2013-09-05 15:50:01 +0530
  Processing by BillingsController#tablebill as HTML
  Parameters: {"utf8"=>"√", "tableno"=>"08", "commit"=>"Enter"}
  List Load (1.0ms)  SELECT itemname,tableno,quantity,amount FROM `lists` WHERE
`lists`.`tableno` = '08'
Completed 500 Internal Server Error in 230ms

NoMethodError (You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.+):
  app/controllers/billings_controller.rb:8:in `block in tablebill'
  app/controllers/billings_controller.rb:7:in `each'
  app/controllers/billings_controller.rb:7:in `tablebill'

Problem is when I execute same query on rails console, its giving results. 问题是当我在Rails控制台上执行相同的查询时,会给出结果。 Below is snippet from rails console 以下是Rails控制台中的代码段

irb(main):073:0> @orders = List.select("itemname,tableno,quantity,amount").where(:tableno => '08')
=> [#<List tableno: "08", itemname: "test81 ", quantity: "1", amount: "
3.25">, #<List tableno: "08", itemname: "Test82", quantity: "1", amou
nt: "3.75">, #<List tableno: "08", itemname: "Test83", quantity: "1
", amount: "3.75">]

I have tried a lot with almost all values present in DB but every time its same when done through view and I get results when executing query through rails console. 我已经尝试了很多几乎所有数据库中都存在的值,但是每次通过视图完成时都是相同的,并且通过Rails控制台执行查询时会得到结果。 I have tried a lot before posting here as i don't think any issue in passing the value. 在发布此处之前,我已经尝试了很多,因为我认为传递值不会有任何问题。 Its just that the select query isn't working. 只是选择查询不起作用。 Please advise. 请指教。 Thanks. 谢谢。

I don't think it's because params[:tableno] is nil I think it is because you haven't initialized @totamount . 我不认为这是因为params[:tableno]为零,我认为这是因为您尚未初始化@totamount Then when you try to add the first order.amount to it you are getting an error because you can't add to nil . 然后,当您尝试向其添加第一个order.amount ,由于无法添加到nil ,您将收到错误消息。

Try either setting @totamount = 0 before your loop or doing: 尝试在循环前设置@totamount = 0或执行以下操作:

@totamount = order.map(&:amount).inject(&:+)

To add all the amounts together. 将所有金额加在一起。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM