前提・実現したいこと
rubyでサービスの開発を行っています。
フォームページの前のページのURLのIDを取得して、フォームページの保存時に、同じDBに保存するやり方を教えて頂けると大変嬉しいです。
具体的に
ある記事(spot)に、投稿(post)を行い、紐付けたいと考えています。
DB上は、spotのid(URL)に表示されているものを、postの投稿フォームで取得して
PostDBのartcle_idカラムに保存したいです。
Ruby
1posts/controller.rb 2 3def new 4 @post = Post.new 5 end 6 7def create 8 @post = Post.new(post_params) 9 @post.user_id = current_user.id 10 11 12 respond_to do |format| 13 if @post.save 14 format.html { redirect_to @post, notice: 'Post was successfully created.' } 15 format.json { render :show, status: :created, location: @post } 16 else 17 format.html { render :new } 18 format.json { render json: @post.errors, status: :unprocessable_entity } 19 end 20 end 21 end
ruby
1views/spots/show.html.erb 2 3<p id="notice"><%= notice %></p> 4 5<p> 6 <strong>Spot name:</strong> 7 <%= @spot.spot_name %> 8</p> 9 10<p> 11 <strong>Content:</strong> 12 <%= @spot.content %> 13</p> 14 15<%= link_to 'Edit', edit_spot_path(@spot) %> | 16<%= link_to 'Back', spots_path %> 17<%= link_to '募集する!', new_post_path %> 18
現在の状況
記事の詳細(show)画面に、投稿する(post/new)のリンクを貼っている状態です。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。