简体   繁体   中英

How to send the value of the e-mail address with Ruby on Rails?

I have a form area like the one below, and if you have an e-mail address, I want a value for that email address sent. But I'm getting an error that there's no value.

I want the value of the e-mail to be e-mailed.

undefined method `value' for nil:NilClass

Extracted source (around line #71):

 rad_check = params[:emailaddress]
 **data = RadCheck.find_by(emailaddress: params[:emailaddress]).value**
 RadCheckMailer.rad_check_forgot(rad_check, data).deliver

rad_checks_controller.rb

def forgot    
  rad_check = params[:emailaddress]
  data = RadCheck.find_by(emailaddress: params[:emailaddress]).value
  RadCheckMailer.rad_check_forgot(rad_check, data).deliver           
end

rad_check_mailer.rb

class RadCheckMailer < ApplicationMailer
    default :from => "test@domain.com"

    def rad_check_forgot(emailaddress, data)
        @rad_check_data = data
        mail(:to => emailaddress, :subject => "Forgot")
    end
end

rad_check_forgot.text.erb

<%= @rad_check_data %>

Tanks!

forgot.html.erb

    <%= form_tag 'forgot' do %>

      <div id="error" class="middle-box text-left loginscreen">
        <%# Error Messages display here! %>
      </div>



      <div class="form-group">
        <%= text_field_tag  "emailaddress" %>
      </div>



      <div class="form-group">              
        <%= submit_tag "submit"%>
      </div>
    </div>
    <% end %>
    <p class="m-t"> <small>Corperation &copy; 2019</small> </p>
  <div>
</div>

RadCheck Table

class CreateRadChecks < ActiveRecord::Migration[5.2]
  def change
    create_table :rad_checks, force: :cascade do |t|

      t.string :value,        :limit => 253, default: "",   null: false      
      t.string :emailaddress


      t.timestamps

    end
    add_index(:rad_checks, :emailaddress, {:name=>:emailaddress, :length=>{:emailaddress=>32}, :using=>"btree"})
  end
end

I think you can't take the column name as "value", it's a reserved keyword. please check it once.

Update: Are you getting params[:emailaddress] value? If it is getting then it should be present in RadCheck table. When you use find_by method it will return nil if condition is false. So when we get nil then there is no scope for nil.vlaue. Thats way your getting that error.

Please check value of params[:emailaddress] existed in table records or not. Try below code.

if params[:emailaddress]
  rad_check = params[:emailaddress]
  data = RadCheck.find_by(emailaddress: params[:emailaddress]).value
  RadCheckMailer.rad_check_forgot(rad_check, data).deliver 
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM