如何构建一个合适的form_tag

时间:2012-06-08 15:35:31

标签: ruby-on-rails ruby ruby-on-rails-3

我想构建一个form_tag,允许我向客户发布新笔记。我的佣金路线如下:

[hchq (master)]$ rake routes
    user_notes GET    /users/:user_id/notes(.:format)          notes#index
               POST   /users/:user_id/notes(.:format)          notes#create
 new_user_note GET    /users/:user_id/notes/new(.:format)      notes#new
edit_user_note GET    /users/:user_id/notes/:id/edit(.:format) notes#edit
     user_note GET    /users/:user_id/notes/:id(.:format)      notes#show
               PUT    /users/:user_id/notes/:id(.:format)      notes#update
               DELETE /users/:user_id/notes/:id(.:format)      notes#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
               PUT    /users/:id(.:format)                     users#update
               DELETE /users/:id(.:format)                     users#destroy
      sessions POST   /sessions(.:format)                      sessions#create
   new_session GET    /sessions/new(.:format)                  sessions#new
       session DELETE /sessions/:id(.:format)                  sessions#destroy
       clients GET    /clients(.:format)                       clients#index
               POST   /clients(.:format)                       clients#create
    new_client GET    /clients/new(.:format)                   clients#new
   edit_client GET    /clients/:id/edit(.:format)              clients#edit
        client GET    /clients/:id(.:format)                   clients#show
               PUT    /clients/:id(.:format)                   clients#update
               DELETE /clients/:id(.:format)                   clients#destroy
          root        /                                        clients#index
        signup        /signup(.:format)                        users#new
        signin        /signin(.:format)                        sessions#new
       signout DELETE /signout(.:format)                       sessions#destroy

我可以在笔记上发布创建操作,还是需要获取new_user_note_path?为什么?我在控制器中有一个@client实例变量。

这就是我目前所拥有的:

<%= form_tag(new_user_note_path, method: :get) do |f| %>
    <%= render 'shared/error_messages', object: f.object %> 
    <div class="field">
        <%= f.text_area :content, placeholder: "Compose new note" %> 
    </div>
<%= f.submit "New Note"%> 
<% end %>

这给我带来了这个错误:

enter image description here

我认为需要发生的是我需要构建一个form_tag,它可以命中控制器的create或new_user_note动作。我认为它需要点击new_user_note,现在我考虑更多,以便它可以设置客户端ID。这是对的吗?

如果是这样,我如何构建我的form_tag?我是否需要使用client_id传递隐藏字段?

另外,关于new和create动作,我的音符控制器应该是什么样的?

2 个答案:

答案 0 :(得分:1)

  1. 由于用户是资源,因此:postusers_notes_path应由notes#create处理,因为它是资源
  2. 同样,由于用户是资源,为什么不是用户form_for? (或simpleform / formtastic / whated适合你)?

答案 1 :(得分:1)

您的用户资源是否映射到用户模型?如果确实如此,那么form_for是您最好的选择。在实例变量

中使用form_for传递时

<%= form_for @user do |f| %>