2016-10-10 63 views
0

我有一個聯繫表單,當我使用URL加載表單頁面時,工作正常。現在我重新設計了網站,表單現在正在側欄中呈現,當我點擊側邊欄中的提交按鈕時,發現發佈錯誤。補充工具欄形式 - 沒有路由匹配[POST]

現在,如果我使用URL加載表單頁面,只要留在表單頁面上,只要我改變頁面,當我點擊時,側欄表單將會拋出'沒有路由匹配[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 

回答

0

我幾乎想通了這一點。

我包裹着我的形式在的form_tag助手這樣的:

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

,並且解決了「沒有路由賽後

然後留在同一頁上提交表單後問題。我修改了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 

感謝堆棧溢出社區,我收集了幾乎所有的東西,從這些頁面中弄清楚了這一點。

感謝所有,

斯科特