简体   繁体   中英

How to pass data to the views Rails

I have form, that send ajax post request to my controller. In my controller I caluculate total price:

def price
  @direction = params[:@directions_ids].to_f
  @kind_of_work = params[:kind_of_work].to_f
  @topic = params[:topic].to_f
  @work_discipline = params[:work_discipline].to_f
  @number_of_pages = params[:number_of_pages].to_f
  @deadline = params[:deadline].to_f
  @singularity = params[:singularity].to_f
  @manual = params[:manual]
  @example = params[:example]
  @total_price = @direction*@kind_of_work*@number_of_pages*@deadline*@singularity
  respond_to do |format|
    format.js
    format.html
  end
end

but i cant use it in my form

<%= f.hidden_field :total_price, :value => @total_price %>

If I understand your question correctly, your price action is handled by the POST request you are making. But you are expecting the instance variable @total_price be available in the form.

You need to set the instance variables in the action that renders the form. Not in the action that processes the form.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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