繁体   English   中英

如何使用方法:“发布”将视图中的表单数据发送到Rails中的其他控制器?

[英]How to use method: 'post' to send form data from view to other controller in rails?

<%= simple_form_for :addcomments, :url => addcomments_post_path, :multipart => true ,:method => 'post' do |f| %>
<%= f.input :title %>
<%=  f.input :body %>
<%= f.button :submit %>

这是我为帖子添加评论的局部视图,下面是我的post-> show视图

<div>
<h4>Comments </h4><br>
<%= @all_comments.each do |t| %>
  <h3><%= t.title %></h3> <br>
  <h4><%= t.body %></h4>   <br>
  <% end %>
  <%= render :partial => 'addcomment' %>
</div>

Comments_controller.rb和

class CommentsController < ApplicationController

# before_filter :load_commentable


def addcomments
@post = Post.find(params[:id])
@user = current_user
@comment = Comment.build_from(@post,@user,params[:comment])
if @comment.save
  redirect_to post_url
else
  redirect_to post_url
end
end
end

Routes.rb

resources :comments do
member  do
  post  'addcomments', to: 'comments#addcomments'
end
end

当我运行此代码并单击post-> show时,出现错误

The action 'addcomments' could not be found for CommentsController

您正在使用多态模型,因此您的第一层路线应该是“帖子”而不是“评论”。

# resources :comments do # Change this line to
resources :posts do
  member do
    post 'addcomments', to: 'comments#addcomments'
  end
end

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM