1
我试图呈现一个_new.html.erb部分从posts控制器内部的意见控制器。渲染嵌套部分新形式
我的评论资源被嵌套在我的职位资源
的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'
我_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 %>
我的评论控制器的方法:
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'
下面是我耙路线
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为零渲染局部时。我猜产品/展示的html输出会使用“/ comments”操作呈现新的评论表单,对吧? – bcd
是的,源代码显示“/评论”。 – user749798
你如何给@post,@comment赋值? – bcd