【发布时间】:2014-12-03 14:36:42
【问题描述】:
我试图弄清楚为什么我的表单提交错误。我正在尝试在帖子下实现嵌套的 cmets,并且我的表单呈现但提交给错误的操作。我已经尝试了一些东西,但无法让它工作。我可以通过 rails 控制台创建评论,但不能通过我的表单。我正在经历一个railscast,他使用的是旧版本的rails。我认为主要区别在于强大的参数,但它似乎不起作用,我无法弄清楚我在这里缺少什么。谢谢。
这是我的日志
Started GET "/posts/1?utf8=%E2%9C%93&authenticity_token=PJmmRV6hnY%2Bgm4cVe5LSdALHezbI3ehMkud0yYTaA%2FQ%3D&comment%5Bname%5D=Mark&comment%5Bemail%5D=hustada80%40gmail.com&comment%5Bcontent%5D=this+is+a+comment&commit=Create+Comment" for 127.0.0.1 at 2014-10-08 14:34:53 -0500
Processing by PostsController#show as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"PJmmRV6hnY+gm4cVe5LSdALHezbI3ehMkud0yYTaA/Q=", "comment"=>{"name"=>"Mark", "email"=>"hustada80@gmail.com", "content"=>"this is a comment"}, "commit"=>"Create Comment", "id"=>"1"}
Post Load (0.2ms) SELECT "posts".* FROM "posts" WHERE "posts"."id" = ? LIMIT 1 [["id", 1]]
Rendered comments/_form.html.erb (4.2ms)
Rendered comments/_comments.html.erb (0.1ms)
Rendered posts/show.html.erb within layouts/application (53.1ms)
Rendered layouts/_header.html.erb (0.5ms)
Rendered layouts/_footer.html.erb (0.1ms)
Completed 200 OK in 524ms (Views: 519.7ms | ActiveRecord: 0.2ms)
这是我的表单(部分_form)
<div class="well">
<h4>Leave a comment</h4>
<form role="form" class="clearfix">
<%= form_for([@commentable, @comment]) do |f| %>
<div class="form-group">
<%= f.label :name %>
<%= f.text_field :name, class: 'form-control' %>
</div>
<div class="form-group">
<%= f.label :email %>
<%= f.text_field :email, class: 'form-control', required: true %>
</div>
<div class="form-group">
<%= f.label :content %>
<%= f.text_area :content, class: 'form-control', required: true %>
</div>
<%= f.submit class: 'btn btn-primary' %>
<% end %>
</div>
帖子控制器
class PostsController < ApplicationController
def new
@post = Post.new
end
def show
@post = Post.find(params[:id])
@commentable = @post
@comments = @commentable.comments
@comment = Comment.new
end
def index
@post = Post.all
@posts = Post.order('created_at DESC')
##@posts_by_month = Post.find(:all, :order => 'created_at DESC').group_by { |post| post.created_at.strftime("%B %Y") }
end
def month_count
@posts_by_month = Post.find(:all, :order => 'created_at DESC').group_by { |post| post.created_at.strftime("%B %Y") }
end
def create
@post = Post.new(post_params)
@post.save
redirect_to @post
end
private
def post_params
params.require(:post).permit(:title, :text)
end
评论控制器
class CommentsController < ApplicationController
before_action :load_commentable
def index
@comments = @commentable.comments
end
def new
@comment = @commentable.comments.new
end
def create
@comment = @commentable.comments.new(comments_params)
if @comment.save
redirect_to @commentable, notice: "Comment created."
else
render :new
end
end
def destroy
end
private
# def load_commentable
# resource, id = request.path.split('/')[1, 2]
# @commentable = resource.singularize.classify.constantize.find(id)
# end
def load_commentable
klass = [Post].detect { |c| params["#{c.name.underscore}_id"]}
@commentable = klass.find(params["#{klass.name.underscore}_id"])
end
def comments_params
params.require(:comment).permit(:content, :email, :name)
end
结束
路线
Rails.application.routes.draw do
root 'posts#index'
get "sign_up" => "users#new", :as => "sign_up"
get "/log-in" => "sessions#new"
post "/log-in" => "sessions#create"
get "/log-out" => "sessions#destroy", as: :log_out
resources :posts do
resources :comments
end
resources :users
resources :sessions
【问题讨论】:
标签: ruby-on-rails forms ruby-on-rails-4 controller partials