Rails5:form_forの使い方をうまく理解していないためなのか、データベースに保存がされません。
routes
ruby
1resources :items do 2 resources :contributions, only: [:create, :destroy] 3end
Controller contributions_controller.erb
ruby
1class ContributionsController < ApplicationController 2 def create 3 @contribution = current_user.contributions.create(contribution_params) 4 redirect_to @contribution.item, notice: "送信しました" 5 end 6 7 8 private 9 def contribution_params 10 params.require(:contribution).permit(:content, :item_id) 11 end 12end
Controller items_controller.erb
ruby
1class ItemsController < ApplicationController 2 def show 3 @item = Item.find(params[:id]) 4 end 5end
View items/show.html.erb
ruby
1<%= form_for([@item,@item.contributions.new], :html => { :multipart => true }) do |f| %> 2 <%= f.text_area :content, required: 'true', rows: 3, class: "form-control" %> 3 4 <%= f.hidden_field :item_id, value: @item.id %> 5 6 <div class="actions row-space-top-2 text-right"> 7 <%= f.submit "送信する", class: "btn btn-default form-send-btn" %> 8 </div> 9<% end %>
状況
・エラー文は出ておりません。
ターミナル
Processing by ItemsController#show as HTML Parameters: {"utf8"=>"✓", "authenticity_token"=>"aw/dYNe/6lC77k0PrdL+E/Z1sczfzWnZZOfsfef8or5k13SF62YifsoeGtX3g2syXzlzgTyVtx3gT5+hqmE8FpvPiwhlWLA==", "contribution"=>{"content"=>"アイウエオ", "item_id"=>"62"}, "commit"=>"送信する", "id"=>"62"} Item Load (0.3ms) SELECT "items".* FROM "items" WHERE "items"."id" = ? LIMIT ? [["id", 62], ["LIMIT", 1]] Rendering items/show.html.erb within layouts/application User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ? [["id", 1], ["LIMIT", 1]] Rendered partial/_sidebar.html.erb (6.1ms) Rendered partial/_navbar.html.erb (2.9ms) Rendered partial/_navmenu.html.erb (0.5ms) User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]] Rendered contributions/_form.html.erb (6.4ms) Rendered items/show.html.erb within layouts/application (39.3ms) Completed 200 OK in 454ms (Views: 443.9ms | ActiveRecord: 0.8ms)
データベースに保存されず、URLがコントローラーで指定したURLではなく、下記のようなURLに返される状況です。
http://localhost:3000/items/62?utf8=%E2%9C%93&authenticity_token=aw%2FdYNe%2F6lC77k0PrdL%hfw2FZ1sczfzWnZjvrZIZOr5k13SF62YiGtX3g2syfewo38ksXzlzgTyVtx3gT5%2BhqmE8FpvPlWLA%3D%3D&contribution%5Bcontent%5D=%E3%82%A2%E3%82%A4%E3%8ei2%A6%E3%82%A882%AA&contribution%5Bitem_id%5D=62&commit=%E9%80%81%E4%BF%A1%E3%81%99%E3%82%8B%d7%s9%
なぜ、こうなってしまうのか分からない状況です。どう調べたら良いのかも分からず、こちらの方で質問をさせていただきました。
ご教授よろしくお願い致します。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/01/16 09:32
退会済みユーザー
2019/01/17 00:15
2019/01/17 06:22
退会済みユーザー
2019/01/17 06:26
2019/01/17 06:36
退会済みユーザー
2019/01/17 06:37
2019/01/17 06:55
退会済みユーザー
2019/01/17 07:14