当我尝试渲染此视图时:
Reply:
<%= form_for :message, url: [:reply, conversation] do |f| %>
<%= f.text_area :body %>
<%= f.submit "Send Message", class: 'btn btn-primary' %>
<%= submit_tag 'Clear Reply Box', type: :reset, class: 'btn btn-danger' %>
<% end %>
我收到错误:undefined method 'reply_mailboxer_conversation_path'
路线:
resources :messages do
member do
post :new
end
end
resources :conversations do
member do
post :reply
post :trash
post :untrash
end
collection do
get :trashbin
post :empty_trash
end
end
rake路线输出:
user_session POST /users/sign_in(.:format) devise/sessions#create
destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
user_password POST /users/password(.:format) devise/passwords#create
new_user_password GET /users/password/new(.:format) devise/passwords#new
edit_user_password GET /users/password/edit(.:format) devise/passwords#edit
PATCH /users/password(.:format) devise/passwords#update
PUT /users/password(.:format) devise/passwords#update
cancel_user_registration GET /users/cancel(.:format) registrations#cancel
user_registration POST /users(.:format) registrations#create
new_user_registration GET /users/sign_up(.:format) registrations#new
edit_user_registration GET /users/edit(.:format) registrations#edit
PATCH /users(.:format) registrations#update
PUT /users(.:format) registrations#update
DELETE /users(.:format) registrations#destroy
users GET /users(.:format) users#index
POST /users(.:format) users#create
new_user GET /users/new(.:format) users#new
edit_user GET /users/:id/edit(.:format) users#edit
user GET /users/:id(.:format) users#show
PATCH /users/:id(.:format) users#update
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy
root GET / profiles#index
message POST /messages/:id(.:format) messages#new
messages 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
GET /messages/:id(.:format) messages#show
PATCH /messages/:id(.:format) messages#update
PUT /messages/:id(.:format) messages#update
DELETE /messages/:id(.:format) messages#destroy
reply_conversation POST /conversations/:id/reply(.:format) conversations#reply
trash_conversation POST /conversations/:id/trash(.:format) conversations#trash
untrash_conversation POST /conversations/:id/untrash(.:format) conversations#untrash
trashbin_conversations GET /conversations/trashbin(.:format) conversations#trashbin
empty_trash_conversations POST /conversations/empty_trash(.:format) conversations#empty_trash
conversations GET /conversations(.:format) conversations#index
POST /conversations(.:format) conversations#create
new_conversation GET /conversations/new(.:format) conversations#new
edit_conversation GET /conversations/:id/edit(.:format) conversations#edit
conversation GET /conversations/:id(.:format) conversations#show
PATCH /conversations/:id(.:format) conversations#update
PUT /conversations/:id(.:format) conversations#update
DELETE /conversations/:id(.:format) conversations#destroy
真的不确定我做错了什么。如果我遗漏了任何重要代码,请告诉我们。
答案 0 :(得分:2)
您收到的错误是因为您定义form_for
块的方式:
<%= form_for :message, url: [:reply, conversation] do |f| %>
来自docs:
以资源为导向的风格
在刚刚显示的示例中,虽然未明确指出,但我们 仍然需要使用:url选项以指定表单的位置 将要发送。但是,如果可以进一步简化 传递给form_for的记录是一种资源,即它对应于a 一组RESTful路由,例如使用资源方法定义 配置/ routes.rb中。在这种情况下,Rails将简单地推断出合适的 记录本身的URL。例如,
<%= form_for @post do |f| %>
...
<% end %>
等同于:
<%= form_for @post, as: :post, url: post_path(@post), method: :patch, html: { class: "edit_post", id: "edit_post_45" } do |f| %>
...
<% end %>
-
<强>嵌套强>
当您为resource
的{{1}}部分使用数组时,您基本上会告诉form_for
将继承conversation
。
我们这样使用它:
reply
这基本上对待<%= form_for [:admin, @object] do |f| %>
属性,如下所示:
url:reply_conversation_path
我不知道它来自哪里url
,但无论哪种方式,问题都会由mailboxer
的声明引起。
-
<强>修正强>
有两种方法可以解决这个问题。
第一种是使用[:reply, conversation]
内的url:
属性,如下所示:
form_for
第二种是使用正确的格式:
<%= form_for [:reply, @conversation], url: reply_conversation_path(conversation) do |f| %>
答案 1 :(得分:1)
每当你制作表格时,你必须记住两件事
一个。在案例回复中,您需要在控制器中初始化您的资源。我假设你正在使用会话控制器的show动作,所以你可以像这样初始化它:
@reply = Message.new # assuming you dont have a reply model and you are using message model as reply
湾您创建资源的路径。如果你看看你的佣金路线
reply_conversation POST /conversations/:id/reply(.:format)对话#recovery 。您可以清楚地看到,您需要进行对话才能在该对话中进行回复,因此您必须在对话控制器的show动作中找到该对话,如下所示:
@conversation = Conversation.find(params[:id])
现在制作表格相对简单
<% form_for @reply, url: reply_conversation_path(@conversation.id) do |f| %>
<%= #your fields %>
<% end %>