【发布时间】:2015-08-18 16:15:17
【问题描述】:
我正在单页布局上创建“联系我”表单。我有大部分代码设置,但似乎无法解决出现的错误。我将在下面放置所有带有错误的代码。注意:我还没有设置邮件程序,一旦正确设置了此代码,我就会这样做:)
#routes
Rails.application.routes.draw do
resources :contacts, only: [:new, :create]
devise_for :users
root 'welcome#index'
end
#form
<%= form_for @contact do |x| %>
<div class="field">
<%= x.label :name %><br />
<%= x.text_field :name %>
<%= x.submit %>
</div>
<% end %>
#ContactsController
class ContactsController < ApplicationController
def new
@contact = Contact.new
end
def create
@contact = Contact.new(contact_params)
if @contact.save
flash[:notice] = "Your message has been sent!"
redirect_to :action => :index
else
flash[:notice] = "Try Again!"
end
end
private
def contact_params
params.require(:contact).permit(:name)
end
end
#Index View
<%= render partial: 'contacts/form' %>
我收到的主要错误是First argument in form cannot contain nil or be empty。我也尝试将@contact = Contact.new 放在欢迎控制器的索引操作中,但这会引发这个问题:Unable to autoload constant Contact, expected .../app/mailers/contact.rb to define it
如果您有任何建议、想法或意见,请告诉我 =) 非常感谢您的帮助,谢谢!
PS:如果需要任何进一步的代码或信息,请询问。有一个包含各种列的联系模型。
更新
我现在在欢迎控制器的索引操作中拥有@contact = Contact.new。最终试图解决我收到的邮件问题。
【问题讨论】:
-
表单代码属于哪个视图页面?
-
表单代码在联系人视图文件夹下的一部分,然后将显示在索引视图上。
-
那么在
index操作中使用@contact = Contact.new是正确的。我猜另一个错误与邮件有关。 -
有道理;但是,那个邮件问题让我陷入了困境。我不知道这意味着什么。我还没有设置邮件程序。我不认为我还必须这样做?
-
尝试将
resources :contact, only: [:new, :create]更改为resources :contacts, only: [:new, :create]
标签: ruby-on-rails ruby forms ruby-on-rails-4