当导航到new_mailer时,ActionMailer before_action正在传递给img src

时间:2017-04-07 03:48:26

标签: ruby-on-rails ruby actionmailer

通知控制器:

  class NotificationsController < ApplicationController
  before_action :set_notification, only: [:show, :edit, :update, :destroy]

  # GET /notifications
  # GET /notifications.json
  def index
    @notifications = Notification.all
  end

  # GET /notifications/1
  # GET /notifications/1.json
  def show
  end

  # GET /notifications/new
  def new
    @notification = Notification.new
  end
  .
  .
  .
private
    # Use callbacks to share common setup or constraints between actions.
    def set_notification
      @notification = Notification.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def notification_params
      params.require(:notification).permit(:name, :email, :subject, :text)
    end
end

单击new_notification_path链接时,所有这些都会打印到控制台

Started GET "/notifications/new" for ::1 at 2017-04-06 23:41:03 -0400
Processing by NotificationsController#new as HTML
Started GET "/notifications/legal2.jpg" for ::1 at 2017-04-06 23:41:03 -0400
Started GET "/notifications/photo1.jpg" for ::1 at 2017-04-06 23:41:03 -0400
Processing by NotificationsController#show as JPEG
Processing by NotificationsController#show as JPEG
  Parameters: {"id"=>"legal2"}
  Rendering notifications/new.html.erb within layouts/application
  Parameters: {"id"=>"photo1"}
  Notification Load (0.5ms)  SELECT  "notifications".* FROM "notifications" WHERE "notifications"."id" = ? LIMIT ?  [["id", 0], ["LIMIT", 1]]
  Notification Load (0.0ms)  SELECT  "notifications".* FROM "notifications" WHERE "notifications"."id" = ? LIMIT ?  [["id", 0], ["LIMIT", 1]]
  Rendered notifications/_form.html.erb (1.5ms)
Completed 404 Not Found in 15ms (ActiveRecord: 0.5ms)


Completed 404 Not Found in 4ms (ActiveRecord: 0.0ms)


  Rendered notifications/new.html.erb within layouts/application (14.9ms)


ActiveRecord::RecordNotFound (Couldn't find Notification with 'id'=legal2):
ActiveRecord::RecordNotFound (Couldn't find Notification with 'id'=photo1):


app/controllers/notifications_controller.rb:69:in `set_notification'
app/controllers/notifications_controller.rb:69:in `set_notification'
Completed 200 OK in 198ms (Views: 186.0ms | ActiveRecord: 0.0ms)


  Rendering C:/RailsInstaller/Ruby2.3.0/lib/ruby/gems/2.3.0/gems/actionpack-5.0.2/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout
  Rendering C:/RailsInstaller/Ruby2.3.0/lib/ruby/gems/2.3.0/gems/actionpack-5.0.2/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout
  Rendering C:/RailsInstaller/Ruby2.3.0/lib/ruby/gems/2.3.0/gems/actionpack-5.0.2/lib/action_dispatch/middleware/templates/rescues/_source.html.erb
  Rendering C:/RailsInstaller/Ruby2.3.0/lib/ruby/gems/2.3.0/gems/actionpack-5.0.2/lib/action_dispatch/middleware/templates/rescues/_source.html.erb
  Rendered C:/RailsInstaller/Ruby2.3.0/lib/ruby/gems/2.3.0/gems/actionpack-5.0.2/lib/action_dispatch/middleware/templates/rescues/_source.html.erb (2.0ms)
  Rendered C:/RailsInstaller/Ruby2.3.0/lib/ruby/gems/2.3.0/gems/actionpack-5.0.2/lib/action_dispatch/middleware/templates/rescues/_source.html.erb (2.0ms)
  Rendering C:/RailsInstaller/Ruby2.3.0/lib/ruby/gems/2.3.0/gems/actionpack-5.0.2/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb
  Rendering C:/RailsInstaller/Ruby2.3.0/lib/ruby/gems/2.3.0/gems/actionpack-5.0.2/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb
  Rendered C:/RailsInstaller/Ruby2.3.0/lib/ruby/gems/2.3.0/gems/actionpack-5.0.2/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (1.0ms)
  Rendered C:/RailsInstaller/Ruby2.3.0/lib/ruby/gems/2.3.0/gems/actionpack-5.0.2/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (1.0ms)
  Rendering C:/RailsInstaller/Ruby2.3.0/lib/ruby/gems/2.3.0/gems/actionpack-5.0.2/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb
  Rendered C:/RailsInstaller/Ruby2.3.0/lib/ruby/gems/2.3.0/gems/actionpack-5.0.2/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (0.5ms)
  Rendered C:/RailsInstaller/Ruby2.3.0/lib/ruby/gems/2.3.0/gems/actionpack-5.0.2/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout (1476.0ms)
  Rendering C:/RailsInstaller/Ruby2.3.0/lib/ruby/gems/2.3.0/gems/actionpack-5.0.2/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb
  Rendered C:/RailsInstaller/Ruby2.3.0/lib/ruby/gems/2.3.0/gems/actionpack-5.0.2/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (1.0ms)
  Rendered C:/RailsInstaller/Ruby2.3.0/lib/ruby/gems/2.3.0/gems/actionpack-5.0.2/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout (1644.6ms)

为什么有两个照片来源作为参数调用NotificationsController#show,为什么只有&#39; new&#39;才会调用set_notification。行动被称为?

1 个答案:

答案 0 :(得分:2)

从视图生成错误。也许您正在尝试根据直接路径设置图像。 使用rails资产辅助方法,如

<%= image_tag asset_path("myimage.jpg"), size: '200x200', class: 'img-responsive' %>

它将使用文件夹(app / assets / images)中图像的资源路径,并且可以在开发和生产中使用已编译的参数进行处理。

希望这会有所帮助......