边栏表格 - 没有路线匹配[POST]

时间:2016-10-10 15:22:18

标签: ruby-on-rails ruby forms routes

我有一个联系表单,当我使用URL加载表单页面时工作正常。现在我重新设计了网站,表单现在正在侧边栏中呈现,当我从侧边栏中点击提交按钮时出现发布错误。

现在,如果我使用URL加载表单页面,只要我保留在表单页面上,侧栏就会起作用,只要我更改页面,侧栏表单就会再次出现“No routes matches [Post]”错误当我点击提交按钮时。

任何帮助将不胜感激!

斯科特

Rails.application.routes.draw do

  root 'static_pages#index'


  get 'home'    => 'static_pages#index'

  get   'video'   => 'static_pages#video'

  get   'help'    => 'static_pages#help'

  get   'about'   => 'static_pages#about'

  get   'services'   => 'static_pages#services'

  get     'accordion' => 'static_pages#accordion'

  get   'social' => 'static_pages#social'

  get   '/messages' => 'messages#new'
  post  '/messages' => 'messages#create'



end

Messages_Controller

class MessagesController < ApplicationController

   def new
    @message = Message.new
   end

  def create
    @message = Message.new(message_params)

    if @message.valid?
      MessageMailer.new_message(@message).deliver
      flash[:notice] = "Your Messages Has Been Sent."
      redirect_to messages_path
    else
      flash[:alert] = "Please Fill in All of the Fields."
      render :new
    end

end

private

  def message_params
    params.require(:message).permit(:name, :email, :subject, :content)
  end
end

1 个答案:

答案 0 :(得分:0)

我几乎想通了这个。

我将表单包装在form_tag帮助器中,如下所示:

<%= form_tag("messages", method: "post", :class => "my-styles") do %>
...my form here...
<% end %>

这解决了&#34; No Routes Match POST 问题。

然后在提交表单信息后保持在同一页面我将MessagesController修改为redirect_to(:back),如下所示:

class MessagesController < ApplicationController


def new
@message = Message.new
end


def create
@message = Message.new(message_params)

if @message.valid?
  MessageMailer.new_message(@message).deliver
  flash[:notice] = "Your Messages Has Been Sent."
  redirect_to(:back)

else
  flash[:alert] = "Please Fill in All of the Fields."
  redirect_to(:back)

end

end


private

def message_params
  params.require(:message).permit(:name, :email, :subject, :content)
end

end

感谢Stack 溢出社区,因为我收集了从这些页面中了解所有内容的所有内容。

全部谢谢,

斯科特