Rails 4.1:未调用控制器操作

时间:2014-08-29 22:02:52

标签: controller ruby-on-rails-4.1

我有以下控制器代码,它在同一个控制器中呈现另一个控制器动作:

  def create
    @school_application = SchoolApplication.new(school_application_params)
    @school_application.program_cost = @school_application.calculate_cost_to_charge(params[:school_application][:program],     params[:school_application][:duration])
    if @school_application.save
      Rails.logger.debug("Hey mufugga")
      render action: 'view'
    else  
      flash.now[:error] = "There was a problem with your application"
      render action: "new"
    end
 end

当调用render action: 'view'相应的视图时,调用view.html.haml但我的Rails.logger.debug行不会在我的服务器日志中打印,并且所有其他变量都未在我的视图中设置(其中使用变量)。任何人都能说出发生了什么事吗?这是视图控制器方法。

  def view
   Rails.logger.debug("Hello")
   @school_application =SchoolApplication.find(params[:id])
   Rails.logger.debug(@school_application)
   @sevic = SchoolApplication.sevic(@school_application.sevic)
   @cost = @school_application.program_cost.to_i + @sevic.to_i 
   Rails.logger.debug(@cost)
   session[:cost] = @cost
  end

N.B。以下是对创建请求的Rails lolg响应以及成功和不成功的logger输出:

Started POST "/application/new" for 127.0.0.1 at 2014-08-29 13:56:56 -0700
Processing by SchoolApplicationsController#create as HTML
  Parameters: {"utf8"=>"✓",   "authenticity_token"=>"cl9CZCC2numQ1aCckWkXizXESByZxOFrb/pl8vhA6YQ=", "school_application"=>{"first_name"=>"d", "family_name"=>"dfg", "sevic"=>"1", "unaccompanied_minor_option"=>"1", "I_20"=>"1", "fls_center"=>"4", "start_date"=>"2014-07-30", "duration"=>"1", "housing_type"=>"22", "health_insurance"=>"1", "transfer_student"=>"1", "comments"=>"d", "gender"=>"dfg", "address"=>"d", "city_state_province"=>"d", "postal_code"=>"d", "country"=>"d", "phone_number"=>"d", "email"=>"d", "date_of_birth"=>"2014-08-05", "arrival_airport"=>"3", "read_everything"=>"1", "country_of_birth"=>"d", "country_of_citizenship"=>"d", "work_with_ad"=>"1", "pay_application_fee_or_full"=>"1", "agency"=>"d", "fax_number"=>"d", "program"=>"7"}, "commit"=>"Continue"}
Unpermitted parameters: unaccompanied_minor_option
  PricePlan Load (0.3ms)  SELECT  "price_plans".* FROM "price_plans"  WHERE "price_plans"."program_id" = 7  ORDER BY "price_plans"."id" ASC LIMIT 1000
   (0.1ms)  BEGIN
  SQL (0.4ms)  INSERT INTO "school_applications" ("I_20", "address", "agency",     "arrival_airport", "city_state_province", "comments", "country", "country_of_birth", "country_of_citizenship", "created_at", "date_of_birth", "duration", "email", "family_name", "fax_number", "first_name", "fls_center", "gender", "health_insurance", "housing_type",  "pay_application_fee_or_full", "phone_number", "postal_code", "program", "program_cost", "    read_everything", "sevic", "start_date", "transfer_student", "updated_at", "work_with_ad")   VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20, $21, $22, $23, $24, $25, $26, $27, $28, $29, $30, $31) RETURNING "id"  [["I_20", "t"], ["address", "d"], ["agency", "d"], ["arrival_airport", "3"], ["city_state_province", "d"], ["comments", "d"], ["country", "d"], ["country_of_birth", "d"], ["country_of_citizenship", "d"], ["created_at", "2014-08-29 20:56:56.726865"], ["date_of_birth", "2014-08-05"], ["duration", 1], ["email", "d"], ["family_name", "dfg"], ["fax_number", "d"], ["first_name", "d"], ["fls_center", "4"], ["gender", "dfg"], ["health_insurance", "t"], ["housing_type", "22"], ["pay_application_fee_or_full", "t"], ["phone_number", "d"], ["postal_code", "d"], ["program", "7"], ["program_cost", "475.00"], ["read_everything", "t"], ["sevic", "t"], ["start_date", "2014-07-30"], ["transfer_student", "t"], ["updated_at", "2014-08-29 20:56:56.726865"], ["work_with_ad", "t"]]
   (0.8ms)  COMMIT
Hey mufugga
  Rendered application/view.html.haml within layouts/application (0.3ms)
  Rendered shared/_top_nav.html.haml (0.1ms)
  Rendered shared/_footer.html.haml (0.4ms)
Completed 200 OK in 40ms (Views: 15.8ms | ActiveRecord: 5.1ms)

1 个答案:

答案 0 :(得分:0)

所以事实证明我误解了render :action =>到底是做什么的。在重新访问rails指南后,我意识到它只渲染了与该动作相对应的视图,这不是我预期的行为,因此没有调用与我正在渲染的视图对应的控制器动作。

通过将redirect_to调用到我正在调用的操作的url来解决我想要做的事情,从而从服务器创建了所需的响应。