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

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

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

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

Q&A

解決済

1回答

967閲覧

Rails5でウェブサイトのコンテンツ更新を行いたい

raitehu

総合スコア13

Ruby on Rails 5

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

0グッド

0クリップ

投稿2018/11/11 09:54

コンテンツを追加・編集・削除する画面を作っています。

はじめて質問します。
現在Rails5でウェブサイトのコンテンツの追加・編集・削除を一つの管理画面で一括管理できるものを作っています。
管理画面にはコンテンツの数だけテキストエリア内にコンテンツ内容が表示されていて、テキストエリアのそばに送信・削除ボタンが存在していて、テキストエリアの内容を編集して送信すると更新されるというものを考えています。
現在、新規コンテンツの追加と既存コンテンツの削除まではできましたが、編集がうまくいきません。
以下にソースコードを示します。

static_page_controller.rb

ruby

1def manage 2 @introductions = Introduction.all 3 @introduction = Introduction.new 4end

manage.html.erb

erb

1<% @introductions.each do |introduction| %> 2<%= render 'introductions/form', introduction: introduction %> 3<br><br><br> 4<% end %> 5<%= render 'introductions/form', introduction: @introduction %>

_form.html.erb

erb

1<%= form_for(introduction, url: intro_path) do |f| %> 2<div class="field"> 3 <b><%= '追加' if introduction.description.nil? %></b><br> 4 <%= f.text_area :description, class: 'form-control' %> 5</div> 6 7<div class="actions"> 8 <%= f.submit "送信", :class=>"btn btn-success", :style=>"display: inline" %> 9 10<% unless introduction.description.nil? then %> 11 <%= link_to '削除', introduction, method: :delete, class: 'btn btn-danger', :style=>"display: inline" %> 12<% end %> 13</div> 14<% end %>

routes.rb

ruby

1Rails.application.routes.draw do 2 resources :introductions 3 root 'static_pages#index' 4 get 'manage', to: 'static_pages#manage' 5 post 'intro', to: 'introductions#create' 6 delete 'intro', to: 'introductions#destroy' 7 patch 'intro', to: 'introductions#update' 8end

introductions_controller.rb

ruby

1class IntroductionsController < ApplicationController 2 before_action :set_introduction, only: [:edit, :update, :destroy] 3 def new 4 @introduction = Introduction.new 5 end 6 7 def create 8 @introduction = Introduction.new(introduction_params) 9 if @introduction.save 10 @introduction = Introduction.new 11 redirect_to manage_url 12 else 13 render 'new' 14 end 15 end 16 17 def edit 18 end 19 20 def update 21 if @introduction.update(introduction_params) 22 redirect_to manage_url 23 else 24 render :edit 25 end 26 end 27 28 def destroy 29 @introduction.destroy 30 redirect_to manage_url 31 end 32 33 private 34 def set_introduction 35 @introduction = Introduction.find(params[:id]) 36 end 37 38 def introduction_params 39 params.require(:introduction).permit(:description, :id) 40 end 41end

ここでmanage.htmlにて送信ボタンを押すと
ActiveRecord::RecordNotFound in IntroductionsController#update
Couldn't find Introduction with 'id'=
エラーが出ます。

##試したこと
scaffoldコマンド等で得られるedit画面ではURLにidを含めますが、そういったことをしていないのが問題なのかなとも思いました。
###隠しフォームを利用する
隠しフォームを使ってidを送信しようと思い、_form.html.erbに以下の一文を追加しました。

<% f.hidden_field :id, :value => introduction.id %>

しかし、エラー画面のParametersのところにも表示されず、行き詰っております。

少し変わった状況かもしれませんが、IDを取得する方法をご教示いただけますでしょうか。

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

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

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

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

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

guest

回答1

0

自己解決

隠しフォームの=が抜けていたため、送信ができていなかったようでした。

<%= f.hidden_field :id, :value => introduction.id %>

として、params[:introduction][:id]としたところ、無事更新ができるようになりました。

投稿2018/11/11 15:19

raitehu

総合スコア13

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問