post请求中的rails错误

时间:2016-05-14 20:54:10

标签: ruby-on-rails ruby ruby-on-rails-4

我正在尝试制作一个信使应用程序,我做错了什么 我得到错误,说没有路线匹配[POST]“/ message / new”

/message_controller.rb

class MessageController < ApplicationController
  def new
    @message=Message.new
  end

  def create
    @message=Message.new(:name,:email,:body)
    if @message.save
      redirect_to "message/view_all"
    end
  end

  def view_all
  end
end

app / views / message / new.html.erb

<%=form_for :message do |f|%>
  <h3>name</h3>
  <%=f.text_field :name %><br>
  <%=f.text_field :email %><br>
  <%=f.text_area :body %>
  <%=f.submit%>
<%end%>

配置/ routes.rb中

Rails.application.routes.draw do
  get 'message/new'
  post "message/create"=>"message#create"
  get 'message/view_all'
end

溶液 在控制器中:

def create
   @message=Message.new(params.require(:message).permit(:name,:email,:body))
   if @message.save
      redirect_to "/message/view_all"
   end
  end

在视图中

<%=form_for @message, url:{action: "create"} do |f|%>
<h3>name</h3>
<%=f.text_field :name %><br>
<%=f.text_field :email %><br>
<%=f.text_area :body %>
<%=f.submit%>
<%end%>
路线中的

post "message/create"=>"message#create"

对不起麻烦

1 个答案:

答案 0 :(得分:1)

在您的路线中看到,您只有get 'message/new',您应该通过以下方式替换您的路线:

Rails.application.routes.draw do
  resources :messages
  get 'message/view_all', to: "messages#view_all"
end

另外,在您的表单中,将:message替换为@message