構造
top → content一覧 → content内容
このようなページ構成になっています。
content内容に作成ページやtalk内容が表示される構造です。
困っていること
talkのcreate時にcontentIDを取得できていないためcontentに紐づいたデータの作成ができない。
下記のroutesの場合11に紐づいたデータを作成したい。
追記
showページを表示していてshowアクションだと値を取得できていますが、createメソットだと値が取得できてません。
実装したい内容はcreateメソットで開いているページのパラメーターを取得したいです。
エラー
Couldn't find Content without an ID
コード
view
<div class="row"> <div class="col-md-4 col-md-offset-4"> <%= form_for @message, :url => {:action => 'create'} do |f| %> <%= text_area :talk, cols: 30,rows:10 %> <%= f.submit "送信" %> <% end %> </div> </div>routes
https://e6b72c2f6ccc49f09722addae273efec.vfs.cloud9.us-east-2.amazonaws.com/messages/11
resources :messages
post "/message_create", to:"messages#create"
controller
def show
@content = Content.find(params[:id])
@message = Message.new
end
def create
content = Content.find(params[:id])
@message = content.messages.build(message_params)
if @message.save
flash[:notice] = "作成できました"
else
render "contents/show"
end
end
private
def message_params
params.require(:message).permit(:talk)
end
試したこと
controllerのデータの取得方法をfind_byなどを利用したりしましたができませんでした。
よろしくお願いいたします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。