【问题标题】:render nested partial for new form为新表单渲染嵌套部分
【发布时间】:2012-05-13 20:08:58
【问题描述】:

我正在尝试从 post 控制器内的 cmets 控制器呈现 _new.html.erb 部分。

我的 cmets 资源嵌套在我的帖子资源中

routes.rb

  resources :users, :only => [:show, :create, :new]

  resources :posts do
    resources :comments
    resources :memorybooks
  end

  root to: 'static_pages#home'

  match '/channel',    to: 'static_pages#channel'
  match 'login', to: 'static_pages#login'
  match '/posts', to: 'posts#new'
  match '/users', to: 'users#new'

我的 cmets 控制器中的 _new.html.erb 部分:

   <%= form_for([@post, @comment]) do |f| %>

      <%= f.label :comment %>
      <%= f.text_field :comment %>

      <p>
      <center>
      <%= f.submit "Submit", class: "btn btn-large btn-primary" %>
      </center>
    <% end %>

我的 cmets 控制器方法:

def create
@post = Post.find_by_id(params[:id])
@comment = @post.comments.build(params[:comment])
    if @comment.save
    redirect_to @current_post
else
    render '/'
end
end

在我的 show.html.erb 文件中,当我使用以下内容时:

<%= render 'comments/new' %>

出现了 _new 部分表单,但是当我发布时,我得到了错误:

No route matches [POST] "/comments"

如果我在 show.html.erb 中使用以下行

<%= render new_post_comment %>

我得到错误:

undefined local variable or method `new_post_comment'

以下是我的 rake 路线

               users POST   /users(.:format)                               users#create
            new_user GET    /users/new(.:format)                           users#new
                user GET    /users/:id(.:format)                           users#show
       post_comments GET    /posts/:post_id/comments(.:format)             comments#index
                     POST   /posts/:post_id/comments(.:format)             comments#create
    new_post_comment GET    /posts/:post_id/comments/new(.:format)         comments#new
   edit_post_comment GET    /posts/:post_id/comments/:id/edit(.:format)    comments#edit
        post_comment GET    /posts/:post_id/comments/:id(.:format)         comments#show
                     PUT    /posts/:post_id/comments/:id(.:format)         comments#update
                     DELETE /posts/:post_id/comments/:id(.:format)         comments#destroy
    post_memorybooks GET    /posts/:post_id/memorybooks(.:format)          memorybooks#index
                     POST   /posts/:post_id/memorybooks(.:format)          memorybooks#create
 new_post_memorybook GET    /posts/:post_id/memorybooks/new(.:format)      memorybooks#new
edit_post_memorybook GET    /posts/:post_id/memorybooks/:id/edit(.:format) memorybooks#edit
     post_memorybook GET    /posts/:post_id/memorybooks/:id(.:format)      memorybooks#show
                     PUT    /posts/:post_id/memorybooks/:id(.:format)      memorybooks#update
                     DELETE /posts/:post_id/memorybooks/:id(.:format)      memorybooks#destroy
               posts GET    /posts(.:format)                               posts#index
                     POST   /posts(.:format)                               posts#create
            new_post GET    /posts/new(.:format)                           posts#new
           edit_post GET    /posts/:id/edit(.:format)                      posts#edit
                post GET    /posts/:id(.:format)                           posts#show
                     PUT    /posts/:id(.:format)                           posts#update
                     DELETE /posts/:id(.:format)                           posts#destroy
                root        /                                              static_pages#home
             channel        /channel(.:format)                             static_pages#channel
               login        /login(.:format)                               static_pages#login
                            /posts(.:format)                               posts#new
                            /users(.:format)                               users#new

任何帮助将不胜感激!谢谢!

【问题讨论】:

  • 在渲染部分时,@post 可能为零。我猜 products/show 的 html 输出会呈现带有动作“/cmets”的新评论表单,对吧?
  • 是的,源代码显示“/cmets”。
  • 如何为@post、@comment 赋值?
  • 不确定您的意思...用户发布新帖子,然后获得帖子 ID。当我在帖子网址上时,我会使用 @post = Post.find_by_id(params[:id]) 获取当前帖子信息...现在我正在尝试对该帖子发表评论...跨度>
  • 您需要确保已填充@post。在 cmets/_new 中添加 和 并查看浏览器中显示的内容

标签: ruby-on-rails nested-forms renderpartial


【解决方案1】:

试试这个

<%= render new_post_comment(@post, @comment) %>

希望对你有帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-10-09
    • 1970-01-01
    • 2015-10-22
    • 1970-01-01
    • 2011-09-05
    • 1970-01-01
    • 2015-09-27
    相关资源
    最近更新 更多