我正在尝试使用rails实现一个简单的私人消息系统,它工作正常但是在构建回复系统时,我得到错误。这是代码:
def show
@message = current_user.messages.find(params[:id])
if @message != nil
@sender = User.find(@message.id).email
@replies = @message.replies
@reply = Reply.new
@reply.message_id = params[:id]
@reply.user_id = current_user.id
else
index_redirect
end
端
def reply
@reply = Reply.new(params[:reply])
if @reply.save
set_flash "Reply sent"
show_redirect
else
set_flash "Error, please try again"
show_redirect
end
end
和视图:
<h1>Messages#show</h1>
<h2>replies</h2>
<div>
<%- @replies.each do |reply| %>
<%= content_tag :span, reply.content %>
<%= content_tag :span, reply.created_at %>
<%= content_tag :span, User.find(reply.user_id).email %>
<% end %>
<%= form_for @reply, :as => :reply, :url => { :action => :reply } do |reply| %>
<%= reply.text_area :content %>
<%= reply.submit "Reply" %>
<% end %>
</div>
最后路线:
resources :messages do
collection do
delete 'destroy'
post 'reply'
end
end
messages DELETE /messages(.:format) messages#destroy
reply_messages POST /messages/reply(.:format) messages#reply
GET /messages(.:format) messages#index
POST /messages(.:format) messages#create
new_message GET /messages/new(.:format) messages#new
edit_message GET /messages/:id/edit(.:format) messages#edit
message GET /messages/:id(.:format) messages#show
PUT /messages/:id(.:format) messages#update
DELETE /messages/:id(.:format) messages#destroy
但是在表单提交中我收到此错误:
没有路线匹配{:action =&gt;“show”,:controller =&gt;“messages”}
感谢。
答案 0 :(得分:1)
我猜,正如您的表单一样,它会向仅接收POST
的操作发送GETs
,这就是您收到错误的原因。
在这种情况下,您需要发送正确的操作create
(因为它接受POST
)或将:method
设置为:get
。
在您看来,以下是将表单更改为get
方法的示例。
<%= form_for @reply, :as => :reply, :url => { :action => :reply }, :html => {:method => :post} do |reply| %>
<%= reply.text_area :content %>
<%= reply.submit "Reply" %>
<% end %>