質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Q&A

解決済

1回答

571閲覧

Railsアプリ制作:ArgumentError in Toppages#index を解決したい。

shijimi_

総合スコア3

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

0グッド

0クリップ

投稿2021/08/01 08:12

編集2021/08/01 11:48

○前提・実現したいこと
Ruby on Railsでチャットアプリを制作しています。

○発生している問題・エラーメッセージ
その中でコメントフォームを表示するページに画面遷移をしたいのですが、

ArgumentError in Toppages#index
Showing /home/ubuntu/environment/englishcommunity/app/views/posts/_posts.html.erb where line #17 raised:

wrong number of arguments (given 4, expected 0..3)
Extracted source (around line #17):

15 <%= link_to "Delete", post, method: :delete, data: { confirm: "You sure?" }, class: 'btn btn-danger btn-sm' %> 16 <% else %> 17 <%= link_to 'Comment', comment_path, {controller: :comment, action: :create}, class: 'btn btn-primary btn-sm' %> 18 <% end %> 19 </div> 20 <div>

Trace of template inclusion: #<ActionView::Template app/views/toppages/index.html.erb locals=[]>

Rails.root: /home/ubuntu/environment/englishcommunity

Application Trace | Framework Trace | Full Trace
app/views/posts/_posts.html.erb:17
app/views/posts/_posts.html.erb:3
app/views/toppages/index.html.erb:12

というエラーが発生しており解決方法がわかりません。

現在実装済みの機能としては、投稿機能、フォロー・アンフォロー機能、ログイン機能等です。

お手数おかけいたしますが、何卒よろしくお願いいたします。

○該当のソースコード

<% if posts.any? %> <ul class="list-unstyled mt-2"> <% posts.each do |post| %> <li class="d-flex"> <img class="rounded me-2 mb-5" src="<%= gravatar_url(post.user, { size: 64 }) %>" alt=""> <div> <div> <%= link_to post.user.name, user_path(post.user), class: "text-decoration-none" %> <span class="text-muted">posted at <%= post.created_at %></span> </div> <div> <p><%= post.content %></p> </div> <div> <% if current_user == post.user %> <%= link_to "Delete", post, method: :delete, data: { confirm: "You sure?" }, class: 'btn btn-danger btn-sm' %> <% else %> <%= link_to 'Comment', comment_path, {controller: :comment, action: :create}, class: 'btn btn-primary btn-sm' %> <% end %> </div> <div> </div> </div> </li> <% end %> </ul> <%== pagy_bootstrap_nav(@pagy) %> <% end %>
<% if logged_in? %> <div class="row"> <aside class="col-sm-4"> <%= form_with(model: @post) do |f| %> <div class="mb-3"> <%= f.text_area :content, class: 'form-control', rows: 5 %> </div> <%= f.submit 'Post', class: 'btn btn-primary w-100' %> <% end %> </aside> <div class="col-sm-8"> <%= render 'posts/posts', posts: @posts %> </div> </div> <% else %> <div class="bg-secondary p-3 p-sm-5 my-4 rounded"> <div class="text-center text-white"> <h1>Welcome to the Global Community</h1> <p> This is a community of people who want to learn a foreign language.<br> Feel free to learn a language by chatting! </p> <%= link_to 'Sign up now!', signup_path, class: 'btn btn-lg btn-primary' %> </div> </div> <% end %>
class CommentsController < ApplicationController before_action :require_user_logged_in def create @post_id = params[:post_id] @comment = current_user.comments.new(comment_params) if @comment.save flash[:success] = 'Posted comment.' redirect_back(fallback_location: root_path) else redirect_back(fallback_location: root_path) end end def destroy @comment.destroy flash[:success] = 'Deleted comment' redirect_back(fallback_location: root_path) end private def comment_params params.permit(:comment_content, :post_id) end end

○試したこと
引数の数が合わない、とのことかと思うのですが、検索してもどこを変えれば引数が合うのかが分からず困っています。お力お貸しいただけますと幸いです。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

winterboum

2021/08/01 11:39

エラーメッセージは全文をtextで載せてください
shijimi_

2021/08/01 11:45

失礼いたしました。修正致しましたので何卒よろしくお願いいたします。
guest

回答1

0

ベストアンサー

恐らくlink_toの書き方が違っているかと思います。
pathで指定する場合はcontrollerの指定はしなくていいです。
逆にcotrollerで指定するときはpathの記述は不要です。
ただ、ルーティングがどのようになっているかわかりませんが、postにネストしているならポストのidを渡してあげる必要があります。

投稿2021/08/01 13:14

J_O

総合スコア143

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

shijimi_

2021/08/02 04:14

ありがとうございます。無事エラーなくなりました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問