Rails 5(ajax):参数数量错误(给定 1,预期 0)
Rails 5(ajax): wrong number of arguments (given 1, expected 0)
我试图在 Rails 5 中通过 ajax 为 Post 创建关联的模型评论,但出现错误。
Posts#show
中的参数错误
显示 /home/mnml/rails/ajax-app/app/views/comments/_form.html.erb 第 2 行出现的位置:
参数数量错误(给定 1,预期 0)
routes.rb
resources :posts, only: [:new, :create, :show, :destroy] do
resources :comments, only: [:new, :create, :show, :destroy]
end
post.rb
belongs_to :user
has_many :comments
comment.rb
belongs_to :user, optional: true
belongs_to :post
posts_controller.rb
def show
@post = Post.find(params[:id])
@comment = @post.comments.build
@comments = @post.comments
end
comments_controller.rb
def new
@post = Post.find(params[:post_id])
@comment = @post.comments.build
end
def create
@post = Post.find(params[:post_id])
@comment = @post.comments.build(comment_params)
@comment.user_id = current_user.id
respond_to do |format|
if @comment.save
format.html { redirect_to @comment.post, notice: 'Comment was successfully created.' }
#format.json { render @comment, status: :created, location: @comment }
format.js
else
format.html { render :new }
format.json { render json: @comment.errors.full_messages, status: :unprocessable_entity }
end
end
end
private
def comment_params
params.require(:comment).permit(:description)
end
posts/show.html.erb
<%= render "comments/form"%>
comments/_form.html.erb
<form>
<%= form_with [@post, @post.comments.build], id: :new_comment do |form| %>
<div class="form-group">
<%= form.label :description %>
<%= form.text_field :description, id: :comment_description, class: "form-control" %>
</div>
<div class="form-group">
<%= form.submit "Comment it", class: "btn btn-default", data: { "disable-with": "Comment is saving..." } %>
</div>
<% end %>
</form>
comments/create.js.erb
$('#comments').prepend('<%= j render(@comment) %>')
我哪里做错了?感谢帮助
您需要在 _form.html.erb
文件中这样调用 form_with
<%= form_with model: [@post, @post.comments.build], id: :new_comment do |form| %>
我试图在 Rails 5 中通过 ajax 为 Post 创建关联的模型评论,但出现错误。
Posts#show
中的参数错误显示 /home/mnml/rails/ajax-app/app/views/comments/_form.html.erb 第 2 行出现的位置:
参数数量错误(给定 1,预期 0)
routes.rb
resources :posts, only: [:new, :create, :show, :destroy] do
resources :comments, only: [:new, :create, :show, :destroy]
end
post.rb
belongs_to :user
has_many :comments
comment.rb
belongs_to :user, optional: true
belongs_to :post
posts_controller.rb
def show
@post = Post.find(params[:id])
@comment = @post.comments.build
@comments = @post.comments
end
comments_controller.rb
def new
@post = Post.find(params[:post_id])
@comment = @post.comments.build
end
def create
@post = Post.find(params[:post_id])
@comment = @post.comments.build(comment_params)
@comment.user_id = current_user.id
respond_to do |format|
if @comment.save
format.html { redirect_to @comment.post, notice: 'Comment was successfully created.' }
#format.json { render @comment, status: :created, location: @comment }
format.js
else
format.html { render :new }
format.json { render json: @comment.errors.full_messages, status: :unprocessable_entity }
end
end
end
private
def comment_params
params.require(:comment).permit(:description)
end
posts/show.html.erb
<%= render "comments/form"%>
comments/_form.html.erb
<form>
<%= form_with [@post, @post.comments.build], id: :new_comment do |form| %>
<div class="form-group">
<%= form.label :description %>
<%= form.text_field :description, id: :comment_description, class: "form-control" %>
</div>
<div class="form-group">
<%= form.submit "Comment it", class: "btn btn-default", data: { "disable-with": "Comment is saving..." } %>
</div>
<% end %>
</form>
comments/create.js.erb
$('#comments').prepend('<%= j render(@comment) %>')
我哪里做错了?感谢帮助
您需要在 _form.html.erb
文件中这样调用 form_with
<%= form_with model: [@post, @post.comments.build], id: :new_comment do |form| %>