0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

ruby 投稿実装機能が出来ない

Posted at

アウトプット
何日も頭を悩ませてるのですが中々できない
param is missing or the value is empty: toppage_params が中々解決できません。

そもそもparamsが存在しない?というのもよくわからないのですが、色々試してみました。
まずは下記原型のコード

top_page_controller.rb
class TopPageController < ApplicationController
  def index
    @toppages = TopPage.all
  end

  def new
    @toppage = TopPage.new
  end

  def create
    TopPage.create(toppage_params)
    redirect_to top_page_index_path
  end

  def show
  end

  private
  def toppage_params
    params.require(:toppage_params).permit(:contents)
  end
end

改善を加えたコードが下記です。

top_page_controller.rb
class TopPageController < ApplicationController
  def index
    @toppages = TopPage.all
  end

  def new
    @toppage = TopPage.new
  end

  def create
    @toppage = TopPage.create(toppage_params)
    redirect_to top_page_index_path
  end

  def show
  end

  private
  def toppage_params
    params.permit(:contents)
  end
end

これでエラーはなくなったのですが案の定データが引き渡されていません。
正直、やる前から何となくそうだろうなーと思ってました。。。。
@toppageはnewメソッドで定義してるやつをそのまま使わなければいけないはずなのでcreateでこれだと再定義しているのか?

いずれにせよ form_for form_tag form_withでストロングパラメーター周りの記述法が若干変わるらしく
そこらへんの構造がよくわからない状態。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?