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

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

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

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

JSON

JSON(JavaScript Object Notation)は軽量なデータ記述言語の1つである。構文はJavaScriptをベースとしていますが、JavaScriptに限定されたものではなく、様々なソフトウェアやプログラミング言語間におけるデータの受け渡しが行えるように設計されています。

Ruby on Rails

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

Q&A

解決済

1回答

1206閲覧

ニュース記事に関するコメント機能を作りたいです。

murohi-08

総合スコア12

Ruby

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

JSON

JSON(JavaScript Object Notation)は軽量なデータ記述言語の1つである。構文はJavaScriptをベースとしていますが、JavaScriptに限定されたものではなく、様々なソフトウェアやプログラミング言語間におけるデータの受け渡しが行えるように設計されています。

Ruby on Rails

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

0グッド

0クリップ

投稿2021/01/08 03:16

ニュース記事に関するコメント機能を作ろうと思っています。ユーザーにのみコメントができるように、その記事の詳細画面へと飛んだ後にコメントできるフォームを作っていたのですが、UrlGenerationError出てshowのパスが紐付けられていない状態です。
試しに「link_to」のヘルパーメソッドの箇所に「url: /news/show」を記述してみましたが、トップページに止まるだけで、変化がありませんでした。
仮説と致しましては、ルーティングのresoucesメソッドにshowアクションが読み取れていないからだと考えていますが、ルーティングのネストをしている以上は記述できません。
どなたか修正箇所が多々あると思うので、見てもらえますでしょうか?恐れ入りますが、ご指摘お待ちしております。

イメージ説明

ターミナル news_comments GET /news/:news_id/comments(.:format) comments#index POST /news/:news_id/comments(.:format) comments#create news_index GET /news(.:format) news#index POST /news(.:format) news#create new_news GET /news/new(.:format) news#new edit_news GET /news/:id/edit(.:format) news#edit news GET /news/:id(.:format) news#show PATCH /news/:id(.:format) news#update PUT /news/:id(.:format) news#update DELETE /news/:id(.:format)
config/routes.rb Rails.application.routes.draw do devise_for :users root "news#index" resources :news do collection do get 'seach' end resources :comments, only: [:index,:create] end end
app/controllers/news_controller.rb class NewsController < ApplicationController require "open-uri" def index news_api_key = ENV["NEWS_API_KEY"] uri = "http://newsapi.org/v2/top-headlines?country=jp&apiKey=#{news_api_key}" article_serialized = open(uri).read @articles = JSON.parse(article_serialized) end def show news_api_key = ENV["NEWS_API_KEY"] uri = "http://newsapi.org/v2/top-headlines?country=jp&apiKey=#{news_api_key}" article_serialized = open(uri).read @articles = JSON.parse(article_serialized) @comment = Comment.new end end
app/views/news/index.html.rb <main class="main"> <div class="inner"> <% if user_signed_in? %> <div class="greeting"> <%= link_to current_user.name, class: :greeting__link%> </div> <% end %> </div> <header> <p class="font">Powered by <a href="https://newsapi.org", class="btn">News API</a></p> </header> <div class="articles"> <% @articles["articles"].each do |article| %> <div class="article"> <div class="title"> <%= link_to article["title"], article["url"] %> </div> <div class="wrapper"> <div class="date"> <%= article["publishedAt"] %> </div> <div class="source"> <%= article["source"]["name"]%> </div> <div class="image"> <%= image_tag article["urlToImage"] || '#', :size =>'240x160' %> </div> <div class="content"> <%= article["description"] %> </div > <% if user_signed_in? %> <div class="comment"> <%= link_to "コメントする", news_path%> #この部分になります </div> <% end %> </div> </div> <% end %> </div> </main>
app/views/news/show.html.rb <main class="main"> <div class="inner"> <div class="articles"> <%= @articles["articles"] %> <div class="article"> <div class="title"> <%= link_to article["title"], article["url"] %> </div> <div class="wrapper"> <div class="date"> <%= article["publishedAt"] %> </div> <div class="source"> <%= article["source"]["name"]%> </div> <div class="image"> <%= image_tag article["urlToImage"] || '#', :size =>'240x160' %> </div> <div class="content"> <%= article["description"] %> </div > </div> </div> </div> <ul class="comments_lists"> <% @comments.each do |comment| %> <li class="comments_list"> <%= comment.text%> <%= link_to "(#{comment.user.name})", news_comments_path, class: :comment_user %> </li> <% end %> </ul> </div> </main>
app/controllers/comments_controller.rb class CommentsController < ApplicationController def index @comments = Comment.all.order(id: "DESC") end def create @comment = Comment.new(comment_params) end private def comment_params params.require(:comment).parmit(:text).merge(user_id: current_user.id) end end
app/models/comment.rb class Comment < ApplicationRecord belongs_to :user validates :text, presence: true end

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

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

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

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

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

guest

回答1

0

ベストアンサー

モデルが news なのが厄介かも。これ単複同型なので、例えば postと比較すると
post_path news_path => showを呼ぶ(引数が要るが)
posts_path news_path => indexを呼ぶ

どちらのpathなのか Railsが勘違いしているかも。
rails route を実行して確認してみてください。

投稿2021/01/08 09:05

winterboum

総合スコア23401

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.46%

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

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

質問する

関連した質問