Rails 5 (ajax): неверное количество аргументов (задано 1, ожидается 0) - PullRequest
0 голосов
/ 16 мая 2018

Я пытался создать связанную модель Комментарий для публикации через ajax в Rails 5, но получил ошибку.

ArgumentError в публикациях # show

Отображение / home / mnml / rails / ajax-app / app / views / comments / _form.html.erb, где поднялась строка # 2:

неверное количество аргументов (задано 1, ожидается 0)

rout.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) %>')

ГдеЯ делаю свою ошибку?Спасибо за помощь

1 Ответ

0 голосов
/ 16 мая 2018

Вам нужно позвонить form_with, как это в вашем _form.html.erb файле

<%= form_with model: [@post, @post.comments.build], id: :new_comment do |form| %>
...