程序问答   发布时间:2022-06-01  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Rails ActionController::RoutingError(没有路由匹配 [POST] "/"):大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决Rails ActionController::RoutingError(没有路由匹配 [POST] "/"):?

开发过程中遇到Rails ActionController::RoutingError(没有路由匹配 [POST] "/"):的问题如何解决?下面主要结合日常开发的经验,给出你关于Rails ActionController::RoutingError(没有路由匹配 [POST] "/"):的解决方法建议,希望对你解决Rails ActionController::RoutingError(没有路由匹配 [POST] "/"):有所启发或帮助;

导轨 6.0.3

我已经使用 gem 'mail_form' 设置了一个静态邮件表单。

我知道它可以正常工作,因为我可以从 Rails 控制台发送电子邮件。

问题出在表单上,​​当我按下提交时,它出现了路由错误。

ActionController::RoutingError (No route matches [POST] "/"):

主页表单

<div class="container">
  <%= form_with model: @contact do |f| %>
    <div class="mb-3">
      <h3><%= f.label :name,class: "label" %></h3>
      <%= f.text_fIEld :name,class: "form-control",placeholder: "Write name in here" %>
      <%= f.text_fIEld :email,placeholder: "Write email in here" %>
      <%= f.text_fIEld :message,placeholder: "Write message in here" %>
      <%= f.submit %>
    </div>
  <% end %>
</div>

routes.rb

resources :contact,only: [:create]

contact.rb

class Contact < MailForm::Base
  attribute :name,valIDate: true
  attribute :email,valIDate: true
  attribute :message
  def headers
    {
      subject: "My Contact Form",to: 'myemail@gmail.com',from: %("#{name}" <#{email}>)
    }
  end
end

contact_controller.rb

class ContactController < ApplicationController
  def create
    @contact = Contact.new()
    @contact.name = params[:name]
    @contact.email = params[:email]
    @contact.message = params[:message]
    if @contact.deliver

      render Json: {message: "Email sent successfully"}
      redirect_to root_path
    else
      render Json: @contact.errors
    end
  end
end

我做错了什么或错过了什么步骤?

编辑控制器

class ContactController < ApplicationController

  def create
    @contact = Contact.new(contact_params)
    if @contact.deliver
      redirect_to root_path
    else
      render Json: @contact.errors
    end
  end

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

已编辑的表单

<div class="container">
  <%= form_for Contact.new,url: contacts_path do |f| %>
    <div class="mb-3">
      <h3><%= f.label :name,placeholder: "Write message in here" %>
      <%= f.submit %>
    </div>
  <% end %>
</div>

编辑 routes.rb

resources :contacts,only: [:create]

解决方法

resources :contacts,only: [:create]

资源应该是复数。

class ContactsController < ApplicationController
  def create
    @contact = Contact.new(contact_params)
    if @contact.deliver
      # You can't both render and redirect 
      render json: { message: "Email sent successfully" }
    else
      render json: @contact.errors
    end
  end

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

参数嵌套在 params[:contact][:name]params[:contact][:email] 等中。使用 strong parameters 将参数列入白名单以进行批量赋值,而不是充当人工编译器。

,

@form 不能自动解析路径,我认为:

 <%= form_with model: @contact do |f| %>
  • 尝试添加一个明确的网址:form_with model: @contact,url: contacts_path
  • 如果使用 resourceresources,还要确保 controllers 总是复数,如@max 所说,否则 Rails 迟早会从我的经验中混淆。另一方面,您的联系人只有一种方法,您可以为此添加手动路由,而不要依赖 Rails 自动命名魔法:
# routes

  post "/contact"

# this should also generate a `contact_path` helper,which you can use in the 

大佬总结

以上是大佬教程为你收集整理的Rails ActionController::RoutingError(没有路由匹配 [POST] "/"):全部内容,希望文章能够帮你解决Rails ActionController::RoutingError(没有路由匹配 [POST] "/"):所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: