小编yaz*_*yaz的帖子

Rails,开发环境和错误页面

我做了一个简单的应用程序,我想测试404,500等http页面的错误.我已经在我的环境/ development.rb中将config.consider_all_requests_local更改为false但我仍然遇到了一些问题,所以我想问你几个问题......

  1. 如果我输入不合适的东西,就像http://localhost:3000/products/dfgdgdgdgfd我仍然看到旧的"未知行动"网站.但是,如果我输入我的电脑的本地IP地址.http://192.168.1.106:3000/products/dfgdgdgdgfd我可以从公共文件夹中看到404错误页面.为什么会这样?

  2. 我知道,如果我将我的小项目部署到某个地方而不是我的应用程序将使用生产模式,如果发生任何错误,将显示404或500页面.但是,如果我想让这些错误页面更具动态性(例如,在使用带有常用产品列表的布局时呈现错误消息)或者只是将它们重定向到主页面,该怎么办?

2.1.我发现的第一个解决方案是在应用程序控制器中使用rescue_from方法:

unless Rails.application.config.consider_all_requests_local
   rescue_from Exception, :with => :render_error
   rescue_from ActiveRecord::RecordNotFound, :with => :render_not_found
   rescue_from AbstractController::ActionNotFound, :with => :render_not_found
   rescue_from ActionController::RoutingError, :with => :render_not_found
   rescue_from ActionController::UnknownController, :with => :render_not_found
   rescue_from ActionController::UnknownAction, :with => :render_not_found
end
.
.
.
private
def render_error exception
  Rails.logger.error(exception)
  redirect_to root_path
  #or 
  # render :controller=>'errors', :action=>'error_500', :status=>500
end

def render_not_found exception
  Rails.logger.error(exception)
  redirect_to root_path
  #or 
  # render :controller=>'errors', :action=>'error_404', :status=>404
end
Run Code Online (Sandbox Code Playgroud)

...但该代码在任何情况下都不起作用.

2.2.第二个解决方案是放置match "*path" , :to => "products#show", …

development-environment http-status-code-404 ruby-on-rails-3

2
推荐指数
1
解决办法
3668
查看次数

Rails 3,Action Mailer,电子邮件内容的附件和验证

我已经制作了一个简单的表单,用于发送带附件的电子邮件.问题是我不知道如何让它在邮件中运行.到目前为止,我发现的所有教程都涵盖了附件文件已经位于服务器某处且我无法找到有关验证电子邮件内容的任何内容的情况.

所以,我有两个问题要问你:

  1. 如何让用户发送包含他们上传的附件的电子邮件?
  2. 如何验证用户的附件输入和扩展?

我的电邮表格......

<div id="form_wrapper">
  <%= form_for(:kontakt, :url => {:action => 'kontakt'}, :html => { :multipart => true }, :remote=>true) do |f| %>
  <ul>
    <li>
      <%= f.label(:email) %>
      <%= f.text_field(:email) %>
    </li>
    <li>
      <%= f.label(:content) %>
      <%= f.text_area(:content, :size => "42x7") %>
    </li>
    <li>
      <%= f.label(:preview, :class=>:preview )%>
      <%= f.file_field :preview %>
    </li>
  </ul>
  <%= image_submit_tag("blank.gif",:id=>"send_email", :class=>"action submit") %>
  <%= link_to("Reset", {:controller=>'frontend',:action => 'index'},:remote => true, :class => 'action reset') %>
 <% end %>
</div>
<%= javascript_include_tag 'jquery.form.js','jquery.rails','jquery.remotipart' …
Run Code Online (Sandbox Code Playgroud)

email validation attachment actionmailer ruby-on-rails-3

0
推荐指数
1
解决办法
3646
查看次数