ruby on rails麻烦发送电子邮件

时间:2014-08-22 03:35:56

标签: ruby-on-rails actionmailer

首先:这是我的第一个问题,所以如果我不是在这里做的话,请让我知道将来如何更好地提问。我对ROR也比较陌生!

问题:

我正在尝试让我的网络应用在我的开发环境中发送电子邮件。它不起作用。我不完全确定为什么。版本:Rails 4,Ruby 2。

当前浏览器问题:

ActionController::ParameterMissing in ProfilesController#update
param is missing or the value is empty: profile

Extracted source (around line #83):81 82 83 84 85

# Never trust parameters from the scary internet, only allow the white list through.
def profile_params
    params.require(:profile).permit(:user_id, :first_name, :last_name, :dob, :email, :mobile, :address, :suburb, :postcode, :city, :state, :country)
end

Application Trace | Framework Trace | Full Trace
app/controllers/profiles_controller.rb:83:in `profile_params'
app/controllers/profiles_controller.rb:45:in `block in update'
app/controllers/profiles_controller.rb:44:in `update'
Request

Parameters:

{"utf8"=>"✓",
 "_method"=>"put",
 "authenticity_token"=>"Px14k13lkQa6d8V61K6dvwLN5xhqjieE8Yk5LjiW1z8=",
 "destination"=>"test@gmail.com",
 "commit"=>"Send Email",
 "id"=>"1"}

路线:

resources :profiles do
    put :email
end

个人资料控制器:

def email
    destination = params[:to]
    share = Share.profile(@profile, destination)
    if destination =~ /@/ && share.deliver
        redirect_to @profile, notice: 'email sent :)'
    else 
        redirect_to @profile, notice: 'email failed :('
    end
end

# Never trust parameters from the scary internet, only allow the white list through.
def profile_params
    params.require(:profile).permit(:user_id, :first_name, :last_name, :dob, :email, :mobile, :address, :suburb, :postcode, :city, :state, :country)
end

分享梅勒:

class Share < ActionMailer::Base
    default_url_options[:host] = "localhost:3000"
    default from: "from@example.com"

    def profile(profile, destination)
        @user = profile.user
        @profile = profile
        mail to: destination, 
             subject: "#{@user.email} sent you stuff")
    end
end

查看/简档/显示:

    <!-- Button trigger modal -->
<button class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal">
  Email
</button>

<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-    labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
        <h4 class="modal-title" id="myModalLabel">Share my Info with someone</h4>
      </div>
      <div class="modal-body">
        <%= form_tag profile_path(@profile), method: :put do %>
          <%= label_tag :destination, "What email address would you like to send this to?" %>
          <%= text_field_tag :destination %>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
        <%= submit_tag "Send Email", class: "btn btn-primary" %>
        <% end %>
      </div>
    </div>
  </div>
</div>

非常感谢任何帮助!我不确定我哪里出错了!干杯

1 个答案:

答案 0 :(得分:0)

您正在调用提交表单的更新操作。尝试更改此行,然后重试:

        <%= form_tag profile_path(@profile), method: :put do %>
to
        <%= form_tag email_path(@profile), method: :put do %>

<强>更新

<强> show.html.erb

<%= form_tag email_profile_path(profile: @profile), method: :put do %>

<强> routes.rb中:

get 'email_profile' => 'profiles#email_profile'

<强>控制器:

def email_profile
    @profile = params[:profile]
    destination = params[:to]
    share = Share.profile(@profile, destination)
    if destination =~ /@/ && share.deliver
        redirect_to @profile, notice: 'email sent :)'
    else 
        redirect_to @profile, notice: 'email failed :('
    end
end