【问题标题】:Clicking submit on a new contact form does nothing在新的联系表单上单击提交没有任何作用
【发布时间】:2018-06-30 09:40:12
【问题描述】:

我正在为一个 ruby​​ 应用程序创建一个联系表单,在填写表单并单击提交后,没有任何反应,没有刷新或任何事情。下面是联系人控制器文件、路由文件和与表单关联的 html 文件。

控制器文件

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

    def create
        @contact = Contact.new(contact_params)

        if @contact.save
            redirect_to new_contact_path, notice: 'Message sent.'
        else
            redirect_to new_contact_path, notice: 'Error occured.'
        end 
    end

    private
        def contact_params
            params.require(:contact).permit(:name, :email, :comments)
        end
end

路由文件

Rails.application.routes.draw do
    resources :contacts
    get '/about' => 'pages#about'
    root 'pages#home'

HTML 文件

<div class='row'>
    <div class="col-md-4 col-md-offset-4">
        <div class='well'
            <%= form_for @contact do |f| %>
                 <div class="form-group">
                  <%= f.label :name %>
                  <%= f.text_field :name, class: 'form-control' %>
                </div>
                <div class="form-group">
                  <%= f.label :email %>
                  <%= f.email_field :email, class: 'form-control' %>
                </div>
                <div class="form-group">
                  <%= f.label :comments %>
                  <%= f.text_area :comments, class: 'form-control' %>
                </div>
                <%= f.submit 'Submit', class: 'btn btn-default' %>
            <% end %>
        </div>
    </div>
</div>

【问题讨论】:

  • Rails 日志显示什么,Javascript 控制台显示什么?您能否在问题正文中提供两者的 sn-ps ?另外,欢迎使用 StackOverflow!
  • 你的 routes.rb 中有 end 吗? + 与@JoshBrody 相同的请求

标签: html ruby-on-rails ruby forms contacts


【解决方案1】:

看起来没什么问题,但你能用&lt;button&gt;标签看看会发生什么吗?

【讨论】:

    【解决方案2】:

    我发现了问题,结果证明我只需要学习更好地阅读并密切关注我的代码。在 HTML 文件中,我的第三次使用“well”类缺少“>”。感谢您的回答和帮助,我只需要更好地查看自己的代码。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-05-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-18
      • 1970-01-01
      相关资源
      最近更新 更多