【问题标题】:Rails 4.1: Controller action not being calledRails 4.1:未调用控制器操作
【发布时间】:2014-09-03 00:57:42
【问题描述】:

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

  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

注意这是 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)

【问题讨论】:

    标签: controller ruby-on-rails-4.1


    【解决方案1】:

    原来我误解了render :action => 的作用。重新访问 rails guides 后,我意识到它只渲染与该操作对应的视图,这不是我预期的行为,因此与我正在渲染的视图对应的控制器操作没有被调用。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-12
      • 1970-01-01
      • 2014-04-13
      • 1970-01-01
      • 2013-06-25
      相关资源
      最近更新 更多