Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
380 views
in Technique[技术] by (71.8m points)

ruby on rails - I don't get why this platformatic mail form contact form isn't working

models/lead.rb

class Lead < MailForm::Base
  attribute :fullname

  def headers
    {
      :subject => "My Contact Form",
      :to => "[email protected]",
      :from => "[email protected]"
    }
  end
end

controllers/lead_form_controller.rb

class LeadFormController < ApplicationController
  def new
    @lead = Lead.new
  end

  def create
    @lead = Lead.new(params[:lead_form])
    @lead.request = request
    @lead.deliver
  end
end

routes.rb

  resources :lead_form

views/listings/show.html.erb

<%= form_for @lead, :url => url_for(:controller => 'lead_form', :action => 'new') do |lead| %>
  <%= lead.text_field :fullname %>
  <%= lead.submit %>
<% end %>

The error when trying to access the show page:

First argument in form cannot contain nil or be empty

On line:

<%= form_for @lead, :url => url_for(:controller => 'lead_form', :action => 'new') do |lead| %>

Any help would be super appreciated, can't stand these mailers :(

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Your @lead needs to be initialised in the ListingsController#show action, since your lead form is in that view. Try adding @lead = Lead.new in the ListingsController#show method.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...