konchanxxx / menta

MENTAのタスク管理用リポジトリ
0 stars 0 forks source link

railsでのform_forでのエラー #52

Closed yoshimitsu41 closed 5 years ago

yoshimitsu41 commented 5 years ago

概要

コードはgithubで共有しています。 greenhillというリポジトリです。

というモデルがありあます。

userとclientは多対多。 clientとcommentは1対多です。 一つのクライアントは複数のコメントを持ちます。

clientのshow.html.erb内でコメントを入力して登録できるようにしたいのですが、エラーが出て解決ができません。

実現したいこと

show.html.erb内でコメントを入力して登録できるようにしたいのですが、エラーが出ています。

困っていることがバグの場合は事象やログ、エラーメッセージをできるだけそのまま(抜粋などしない)下記に記載してください。

image

問題となっているアプリケーションのGitHub URL

https://github.com/yoshimitsu41/greenhill.git

konchanxxx commented 5 years ago

https://qiita.com/yoskmr/items/cc8a2dc51c928aa1c87a

とかですかね? 実行したいアクションを指定してあげれば良いかなと思いました:bow:

soipon05 commented 5 years ago

@yoshimitsu41 もしまだ解決されていないようでしたらGitHubのソースは404で見れないのですが、Controllerのcreateアクションのリダイレクト先に問題があるのではないでしょうか?

yoshimitsu41 commented 5 years ago

修正したのですが新たなエラーが出て解決しておりません。 image

#comments_controller.rb
  def create
    @comment = Comment.new(comment_params)

    respond_to do |format|
      if @comment.save
        format.html { redirect_to @comment, notice: 'Comment was successfully created.' }
        format.json { render :show, status: :created, location: @comment }
      else
        format.html { render :new }
        format.json { render json: @comment.errors, status: :unprocessable_entity }
      end
    end
  end
#省略

    def comment_params
      params.require(:comment).permit(:body, :user_id, :client_id)
    end
#view/clients/show.html.erbのフォーム部分
  <% if current_user %>
    <%= form_tag("/clients/#{@client.id}/comments", method: :post) do %>
      <textarea cols="30" name="body" placeholder="コメントする" rows="2"></textarea>
      <input type="submit" value="SENT">
    <% end %>
  <% end %>

エラー画面のparametersではuser_idが渡っていない?のでエラーが出ているのでしょうか?

{"utf8"=>"✓", "authenticity_token"=>"XZ9vaSGlZEHaq3tkKpGBdRn49/VzF/DbVguoRLb4vygiZDW/fvzhvm7cunAw/nvhONsEjde2y8ABoaWS/FeLKA==", "body"=>"a", "client_id"=>"2"}

ご教授お願いします。

soipon05 commented 5 years ago

form_tagを使った経験がないので記法がわからないのですが、Parameterがコメントに紐づいていないからだと思います。 なのでcomment_paramsは中身がないよって書いてるのではないのかな?と思ったので他の方が回答するまではそのあたりで調べてみたらどうでしょうか? respond to doの前にpry-byebugを使って実際に中身を確認してみたらよりはっきりわかるかもです! https://teratail.com/questions/82506 自分も勉強中の身ですが頑張ってください!

soipon05 commented 5 years ago

ちなみに掲示板機能にコメントを残すという感じのアプリを起動させて中見てみたら Parameters: { "utf8" => "✓", "authenticity_token" => "DR3pGYQ158Vl6oFH/RZDZsfbvg2dy901REnfZCsvqD1BQ6BNzs3x3YMpqiM01B+qEB5chvP1HOYHXR0nraa/2g==", "comment" => { "board_id" => "1", "name" => "test", "comment" => "commetn" }, このような表記になっていたのでauthenticity_tokenの後を見ればわかるかと思いますがモデルに紐づいていないというのが濃厚だと思います!

yoshimitsu41 commented 5 years ago
#app/views/comments/_form.thml.erb
<%= form_for @comment, :html => { :class => "form-horizontal comment" } do |f| %>
→
<%= form_for @comment, url:clients_path, :html => { :class => "form-horizontal comment" } do |f| %>

で解決いたしました!

soipon05 commented 5 years ago

参考に伺いたいんですけどform_tagからform_forにした感じですか?

yoshimitsu41 commented 5 years ago

参考に伺いたいんですけどform_tagからform_forにした感じですか?

form_forに変更しました!

<%= form_for [@client, @comment] do |f| %>
  <%= f.text_field :body, :class => 'form-control' %>
  <br>
  <%= f.submit 'コメントする', :class => 'btn btn-primary'  %>
<% end %>
konchanxxx commented 5 years ago

おお、回答ありがとうございます:bow: 後続のエラーはform_tagでリクエストボディに該当するjson keyがないのでparameterをparseできずにエラーになってますね:cry: 基本的にmodelの属性(attribute)に紐付くフォームを生成するときはform_forで良いと思いますmm

あと気になったのはデータ構造的に

user 1 - comments user - * clients

としているのでルーティングをわざわざ

  resources :clients do
    resources :comments
  end

としてclient情報がないとcommentが作れないようにしなくても client => user => comment と辿れるはずなので

resources :clients
resources :comments

とかにした方がデータ同士が疎結合になりシンプルなのかなと思ったりしました。要件ちゃんと把握できてないですが:bow:

konchanxxx commented 5 years ago

こちらcloseしやすー:bow: