在Rails中联系表单

时间:2013-02-08 03:28:29

标签: ruby-on-rails routes

这是一个非常基本的问题,但我很难将其纳入我的应用程序。我在SO上查看了其他类似的问题,但他们都将联系页面作为一个单独的页面使用。

我有一个Rails应用程序的前端,它只是一个面向公众的小型网站,使用stellar.js进行视差滚动。

在页面的最后部分,我想要一个“联系我们”表格,但我得到了

NoMethodError in Welcome#index undefined method `model_name' for NilClass:Class

以下是相关文件:

的routes.rb

TestApp::Application.routes.draw do
   get "welcome/index"
   root :to => 'welcome#index'
   match 'contact' => 'contact#new', :as => 'contact', :via => :get
   match 'contact' => 'contact#create', :as => 'contact', :via => :post

应用程序/视图/欢迎/ index.html.erb

<%= render "contact" %>

页面app / views / welcome / _contact.html.erb的联系部分的部分

 <%= form_for @message, :url => contact_path do |form| %>
  <fieldset class="fields">
     <div class="field">
        <%= form.label :name %>
        <%= form.text_field :name %>
     </div>

     <div class="field">
        <%= form.label :email %>
        <%= form.text_field :email %>
     </div>
     <div class="field">
        <%= form.label :subject %>
        <%= form.text_field :subject %>
     </div>

     <div class="field">
        <%= form.label :body %>
        <%= form.text_area :body %>
     </div>
  </fieldset>

  <fieldset class="actions">
     <%= form.submit "Send" %>
  </fieldset>
  <% end %>

应用程序/控制器/ contact_controller.rb

class ContactController < ApplicationController
   def new
      @message = Message.new
   end

   def create
      @message = Message.new(params[:message])

      if @message.valid?
         NotificationsMailer.new_message(@message).deliver
         redirect_to(root_path, :notice => "Message was successfully sent.")
      else
         flash.now.alert = "Please fill all fields."
         render :new
      end
   end
end

message.rb

class Message

   include ActiveModel::Validations
   include ActiveModel::Conversion
   extend ActiveModel::Naming

   attr_accessor :name, :email, :subject, :body

   validates :name, :email, :subject, :body, :presence => true
   validates :email, :format => { :with => %r{.+@.+\..+} }, :allow_blank => true

   def initialize(attributes = {})
     attributes.each do |name, value|
         send("#{name}=", value)
     end 
   end

   def persisted?
      false
   end
end

问题是我需要做什么才能使此表单在welcome#index视图中正常运行?

2 个答案:

答案 0 :(得分:2)

您需要在每个需要@message = Message.new的操作中定义@message

这意味着您需要WelcomeController

def index
  @message = Message.new
end

快捷方式是将其添加到部分的顶部,以防它未设置:

<% @message ||= Message.new %>

(但你需要决定在你的观点中混合对模型的调用是否是你的风格。有些开发人员对此没有问题,有些人也有。)

答案 1 :(得分:1)

您可以更改以下内容:

在路线中:

卸下:

match 'contact' => 'contact#new', :as => 'contact', :via => :get
match 'contact' => 'contact#create', :as => 'contact', :via => :post

添加:

resources :contact   # It will add 7 default REST routes and standard practice model name should be plural if you created your model with singular then keep it singular 

_contact.html.erb部分form path需要new_contact_path

<%= form_for Message.new, :url => contact_path do |form| %>

班级Message应该继承ActiveRecord::Base

class Message < ActiveRecord::Base

end 

create行动中,其他部分应为:

if @message.valid?
     NotificationsMailer.new_message(@message).deliver
     redirect_to(root_path, :notice => "Message was successfully sent.")
else
     flash.now.alert = "Please fill all fields."
     render :template => "welcome/index"
end 
相关问题