操作邮件before_action在导航到new_mailer时传递 img src



通知控制器:

  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 以两个照片源作为参数,如果只调用"new"操作,为什么要调用set_notification?

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

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

它将使用文件夹中图像的资产路径(应用程序/资产/图像(,并将使用编译的配置文件进行开发和生产(

希望这有帮助...

最新更新