使用 Rails MailForm Gem 时的错误消息 - 未定义的方法 'contacts_path

Error message when using Rails MailForm Gem - undefined method 'contacts_path

我们正在尝试使用 Rails MailForm Gem 为我们应用程序中的每个露营地实例生成一个联系表格,以便在用户完成表格后发送一封电子邮件直接从表格到营地所有者。

我们已将联系人资源嵌套在 routes.rb 中的站点资源中 - 以确保表单位于路径 - localhost:3000/sites/1/contacts/new 而不是 localhost:3000/contacts/new.

我们的应用程序中是否缺少某些关键的东西,或者我们应该以不同的方式解决这个问题?不胜感激。

我们的代码是

resources :campsites do resources :contacts end

但是,我们在尝试查看此路径时收到错误消息。报告的错误状态 undefined method "contacts_path' for #<#<Class:0x007ff0ed9527d0>:0x007ff0ed950de0> 我们不确定原因。

我们一直在关注这个演练 (https://rubyonrailshelp.wordpress.com/2014/01/08/rails-4-simple-form-and-mail-form-to-make-contact-form/)。

我们已经查看了 gem 和 Rails 文档,但在多次尝试后我仍然无法查看表单,我们不确定是否与哪里有关我们正在尝试在应用程序中查看表单。

当运行localhost:3000/sites/1/contacts/new时,我们收到以下错误:

NoMethodError in Contacts#new
Showing /Users/elinnet/makers/week11/campFri/app/views/contacts/new.html.erb where line #4 raised:

undefined method 'contacts_path' for #<#<Class:0x007ff0ed9527d0>:0x007ff0ed950de0>
Extracted source (around line #4):

(1) <h3>Send A message to Us</h3>

(2) <%= simple_form_for @contact, :html => {:class => 'form-horizontal' } do |f| %>
(3) <%= f.input :name, :required => true %>
(4) <%= f.input :email, :required => true %>
(5) <%= f.input :message, :as => :text, :required => true %>

联系人的控制器是:

class ContactsController < ApplicationController
    def new
     @contact = Contact.new
    end

    def create
     @contact = Contact.new(params[:contact])
     @contact.request = request
     if @contact.deliver
       flash.now[:notice] = 'Thank you for your message. We will contact you soon!'
     else
       flash.now[:error] = 'Cannot send message.'
       render :new
     end
   end
end

联系人的模型是:

class Contact < MailForm::Base
   attribute :name, :validate => true
   attribute :email, :validate => /\A([\w\.%\+\-]+)@([\w\-]+\.)+([\w]{2,})\z/i
   attribute :message
   attribute :nickname, :captcha => true


  def headers
  {
  :subject => "My Contact Form",
  :to => "your_email@example.org",
  :from => %("#{name}" <#{email}>)
  }
  end
end

项目Github参考是:https://github.com/elinnet/camping.git

你的路线是

resources :campsites do 
  resources :contacts 
end

因此,您必须像这样将数组中的两个参数传递给您的表单:

<%= simple_form_for [@campsite, @contact], :html => {:class => 'form-horizontal' } do |f| %>

控制器中的新操作应该类似于:

def new
 @campsite = Campsite.find(params[:campsite_id]
 @contact = @campsite.contacts.build
end

您需要在 simple_form_for 方法中传入 url

<%= simple_form_for @contact, url: correct_url_here :html => {:class => 'form-horizontal' } do |f| %>

运行 rake routes 找到正确的 url:

这是 url 来自文档的摘录。

:url - The URL the form is to be submitted to. This may be represented in the same way as values passed to url_for or link_to. So for example you may use a named route directly. When the model is represented by a string or symbol, as in the example above, if the :url option is not specified, by default the form will be sent back to the current url (We will describe below an alternative resource-oriented usage of form_for in which the URL does not need to be specified explicitly).

如果您想自己查看,这里是 link