1

当我尝试渲染此视图时:

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

耙路线输出:

         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

真的不知道我做错了什么。如果我遗漏了任何重要的代码,请告诉我。

https://github.com/portOdin/gfi2/tree/june6/app/views

4

2 回答 2

2

您收到的错误是因为您定义form_for块的方式:

<%= form_for :message, url: [:reply, conversation] do |f| %>

文档

资源导向风格

在刚刚显示的示例中,虽然没有明确指出,但我们仍然需要使用 :url 选项来指定表单将被发送到哪里。但是,如果传递给 form_for 的记录是资源,则可以进一步简化,即它对应于一组 RESTful 路由,例如使用 config/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一部分时form_for,您基本上是在告诉 railsconversation将继承自reply.

我们像这样使用它:

<%= form_for [:admin, @object] do |f| %>

这基本上是这样对待url属性的:

网址:reply_conversation_path

我不知道它是从哪里来mailboxer的,但无论哪种方式,问题都会由[:reply, conversation].

--

使固定

有两种方法可以解决此问题。

第一种是使用url:里面的属性form_for,像这样:

<%= form_for [:reply, @conversation], url: reply_conversation_path(conversation) do |f| %>

二是使用正确的格式:

<%= form_for @reply, url: reply_conversation_path(@conversation) do |f| %>
   ...
<% end %>
于 2014-06-07T07:15:21.497 回答
1

制作表格时必须牢记两件事

一个。您需要在您的控制器中初始化您的资源,在您的案例回复中。我假设您正在显示对话控制器的操作,因此您可以像这样初始化它:

@reply = Message.new   # assuming you dont have a reply model and you are using message model as reply

湾。创建资源的路径。如果您查看您的 rake 路线

reply_conversation POST /conversations/:id/reply(.:format) conversations#reply。您可以清楚地看到您需要进行对话才能在该对话中做出回复,因此您必须再次在对话控制器的显示操作中找到该对话,如下所示:

@conversation = Conversation.find(params[:id])

现在做一个表格比较简单

<% form_for @reply, url: reply_conversation_path(@conversation.id) do |f| %>
  <%= #your fields %>
<% end %> 
于 2014-06-07T06:37:13.490 回答