【问题标题】:Contact form in RailsRails 中的联系表
【发布时间】:2013-02-08 03:28:29
【问题描述】:

这是一个非常基本的问题,但我很难将它整合到我的应用程序中。我查看了关于 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

app/views/welcome/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 %>

app/controllers/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 视图中正常运行?

【问题讨论】:

    标签: ruby-on-rails routes


    【解决方案1】:

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

    这意味着你需要这个WelcomeController

    def index
      @message = Message.new
    end
    

    一个快捷方式是将它添加到部分的顶部,以防它没有设置:

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

    (但是你需要决定在你的视图中混合调用模型是否是你的风格。有些开发者对此没有问题,有些则有。)

    【讨论】:

    • 谢谢克里斯,效果很好!但是,当我单击提交按钮时,没有任何反应。如果我创建另一个视图 app/views/contact/new.html.erb 并将部分代码放入其中,然后转到:localhost:3000/contact,它将按预期工作。你碰巧知道是什么原因造成的吗?谢谢!
    • “什么都没有发生”是什么意思?您是否再次获得空白屏幕或表单屏幕?
    • 当我单击提交按钮时,它只是位于同一页面上。消息没有发送,没有黑屏,什么都没有。
    • 会不会和部分:url =&gt; contact_path有关?
    • 一种可能性是它不是POSTing 数据并且正在抓取错误的路线。我认为 Simple Form 会默认发布。提交表单时控制台中是否显示POST /contactGET /contact
    【解决方案2】:

    您可以更改以下内容:

    在路线中:

    删除:

    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 action else 部分应该是:

    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 
    

    【讨论】:

    • 这不能回答问题/问题。
    • 他使用ActiveModel技术,联系表格不需要数据库存储。
    猜你喜欢
    • 1970-01-01
    • 2016-12-09
    • 2015-03-18
    • 1970-01-01
    • 2014-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-24
    相关资源
    最近更新 更多