詰まっているところ
クリックしたデータを保持するdb(user_idとpost_idカラムがある)のpost_idが正常に入らない。
id|user_id|post_id|created_at|updated_at
45|26|2|2019-12-16 05:32:28.576273|2019-12-16 05:32:28.576273
post_idはクリックしたidではなく、いつも「2」になってしまう。
たくさん載せて申し訳ないのですが、どうぞよろしくお願い致します。
index.html.erb
1<body id="posts-index"> 2 <div class="container-fluid"> 3 4 <div class="posts-page row"> 5 <%= form_tag("/accepted/#{@current_user.id}/accepted",method: :post,data:{confirm:"このクエストを受けますか?"}) do %> 6 <% @posts.each do |post| %> 7 <div class="posts-left col-md-6"> 8 <div class="posts-content row"> 9 <div class="posts-content-left col-md-6"> 10 <p>受付No</p> 11 <input type="text" name="post_id" required readonly value="<%= post.id %>" class="content-waku"> 12 13 <p>受付日時</p> 14 <h6 class="content-waku"><%= post.created_at %></h6> 15 <p>タイトル</p> 16 <h6 class="content-waku"><%= post.title %></h6> 17 <p>ジャンル</p> 18 <h6 class="content-waku"><%= post.category %></h6> 19 <p>報酬</p> 20 <h6 class="content-waku"><%= post.money %>円</h6> 21 <p>仕事日</p> 22 <h6 class="content-waku"><%= post.jobDate %></h6> 23 <p>性別(求める性別)</p> 24 <h6 class="content-waku"><%= post.radio %></h6> 25 <p>クエスト場所</p> 26 <h6 class="content-waku"><%= post.where %></h6> 27 <p>キャンセル締切日</p> 28 <h6 class="content-waku"><%= post.cancel %></h6> 29 <% if post.img != nil %> 30 <p>画像</p> 31 <img src="<%= "/post_images/#{post.id}.jpg" %>" class="content-waku" > 32 <%else%> 33 <p>画像</p> 34 <img src="<%= "/post_images/brawn.jpg" %>" class="content-waku" > 35 <%end%> 36 </div> 37 <div class="posts-content-right col-md-6"> 38 <p>依頼者ID</p> 39 <h6 class="content-waku"><%= post.user_id %></h6> 40 41 <p>依頼者</p> 42 <h6 class="content-waku"><%= post.postedname %></h6> 43 <p>クエストの内容</p> 44 <h6 class="content-waku"><%= post.content %></h6> 45 <button id="btn1" type="submit" class="btn btn-success btn-lg ">クエストを受ける</button> 46 47 </div> 48 </div> 49 </div> 50 <% end %> 51 <% end %> 52 53 </div> 54 </div> 55 <%= yield %> 56
AcceptController
1class AcceptController < ApplicationController 2before_action:authenticate_user,{only:[:acceptednew,:accepted]} 3before_action:ensure_current_user,{only:[:acceptednew,:accepted]} 4 5 6def acceptednew 7 @accept = Accept.new( 8 user_id: @current_user.id, 9 post_id: params[:post_id] 10 ) 11 @accept.save 12 redirect_to("/users/#{@current_user.id}") 13end 14 15 16def accepted 17 @user = User.find_by(id: params[:id]) 18 @accepteds = Accept.where(user_id: @user.id) 19end 20 21 22 23end 24
PostsController
1class PostsController < ApplicationController 2 before_action:set_post,only: [:show,:edit,:update,:destroy] 3 before_action:authenticate_user,{only:[:index,:show,:edit,:update,:destroy,:questpage]} 4 5 def kensaku 6 end 7 8 9 def index 10 if params[:q] 11 @search = Post.ransack(params[:q]) 12 @posts = @search.result(distinct: true) 13 14 else 15 @posts = Post.all.order(created_at: :desc) 16 end 17 end 18 19 def new 20 @post = Post.new 21 end 22 23 def create 24 @post = Post.new(post_params) 25 @post.user_id = @current_user.id 26 @post.save 27 if params[:img] 28 @post.img = "#{@post.id}.jpg" 29 image = params[:img] 30 File.binwrite("public/post_images/#{@post.id}.jpg",image.read) 31 else 32 @post.img = nil 33 end 34 if @post.save 35 flash[:notice] = "登録に成功しました" 36 redirect_to("/posts/index") 37 else 38 render("/posts/new") 39 end 40 end 41 42 def post_params 43 params.permit(:title, :category, :money, :jobDate, :radio, :where, :cancel, :img, :postedname, :content,:user_id) 44 end 45 46 def show 47 @post = Post.find_by(id:params[:id]) 48 end 49 50 def edit 51 @post = Post.find_by(id:params[:id]) 52 end 53 54 55 def update 56 @post.update(post_params) 57 if params[:img] 58 @post.img = "#{@post.id}.jpg" 59 image = params[:img] 60 File.binwrite("public/post_images/#{@post.id}.jpg",image.read) 61 else 62 @post.img = nil 63 end 64 if @post.save 65 flash[:notice] = "編集に成功しました" 66 redirect_to("/posts/index") 67 else 68 render("/posts/edit") 69 end 70 end 71 72 73 def destroy 74 @post.destroy 75 flash[:notice] = "削除に成功しました" 76 redirect_to("/users/#{@current_user.id}") 77 end 78 79 def set_post 80 @post = Post.find_by(id:params[:id]) 81 end 82 83 def questpage 84 end 85 86 87 88 89end 90 91コード
routes
1get "posts/index" => "posts#index" 2 get "posts/new" => "posts#new" 3 post "posts/create" => "posts#create" 4 get "posts/questpage" => "posts#questpage" 5 get "posts/kensaku" => "posts#kensaku" 6 get "posts/:id" => "posts#show" 7 get "posts/:id/edit" => "posts#edit" 8 post "posts/:id/update"=> "posts#update" 9 post "posts/:id/destroy" => "posts#destroy" 10 11get "login" => "users#login" 12 post "users/login" => "users#loginafter" 13 get "signup" => "users#new" 14 post "users/create" => "users#create" 15 get "users/index" => "users#index" 16 get "users/:id" => "users#show" 17 post "users/:id" => "users#destroy" 18 post "logout" => "users#logout" 19 get "users/:id/edit" => "users#edit" 20 post "users/:id/update" => "users#update" 21 get "users/:id/posted"=> "users#posted" 22 23 post "accepted/:id/accepted"=>"accept#acceptednew" 24 get "accepted/:id/accepted"=>"accept#accepted" 25end
Schema.rb
1create_table "accepts", force: :cascade do |t| 2 t.integer "user_id" 3 t.integer "post_id" 4 t.datetime "created_at", null: false 5 t.datetime "updated_at", null: false 6 end 7 8 create_table "posts", force: :cascade do |t| 9 t.string "title" 10 t.string "category" 11 t.string "where" 12 t.date "cancel" 13 t.text "content" 14 t.datetime "created_at", null: false 15 t.datetime "updated_at", null: false 16 t.string "radio" 17 t.string "jobDate" 18 t.string "img" 19 t.string "money" 20 t.string "postedname" 21 t.string "postedpic" 22 t.integer "user_id" 23 end 24 25 create_table "users", force: :cascade do |t| 26 t.string "user_name" 27 t.string "password_digest" 28 t.string "email" 29 t.datetime "created_at", null: false 30 t.datetime "updated_at", null: false 31 t.string "imagename" 32 end
試して欲しいことがあるのですが、
<p>受付No</p>
<input type="text" name="post_id" required readonly value="<%= post.id %>" class="content-waku">
のvalueに<%= post.id %>ではなく適当な数値を入れた場合は、DBに保存されるデータは何になるでしょうか?
適当に数字を入れると、その数字がでます。
<input type="text" name="post_id" required readonly value="<%= post.id %>" class="content-waku">
↓
<input type="text" name="post_id" required readonly value="#{post.id}" class="content-waku">
valueの部分を上記のように変更してください。
そのようにしたら、
そのまま{post.id}が表示されてしまいました。
すみません。上のは一旦忘れてください。
前にも話が出ていましたが、ログがどうなっているのか気になります。
[$rails s]をしているところ(macを使っているならターミナル)に出てくる文字列を確認してみてください。
送信ボタンをおした後、以下のような文字列が出力されていると思います。(Railsドキュメントより引用)
```
Started POST "/articles" for 127.0.0.1 at 2017-08-20 20:53:10 +0900
Processing by ArticlesController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"xhuIbSBFytHCE1agHgvrlKnSVIOGD6jltW2tO+P6a/ACjQ3igjpV4OdbsZjIhC98QizWH9YdKokrqxBCJrtoqQ==", "article"=>{"title"=>"Debugging Rails", "body"=>"I'm learning how to print in logs!!!", "published"=>"0"}, "commit"=>"Create Article"}
New article: {"id"=>nil, "title"=>"Debugging Rails", "body"=>"I'm learning how to print in logs!!!", "published"=>false, "created_at"=>nil, "updated_at"=>nil}
(0.1ms) BEGIN
SQL (0.4ms) INSERT INTO "articles" ("title", "body", "published", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5) RETURNING "id" [["title", "Debugging Rails"], ["body", "I'm learning how to print in logs!!!"], ["published", "f"], ["created_at", "2017-08-20 11:53:10.010435"], ["updated_at", "2017-08-20 11:53:10.010435"]]
(0.3ms) COMMIT
Redirected to http://localhost:3000/articles/1
Completed 302 Found in 4ms (ActiveRecord: 0.8ms)
```
3行目の「Parameters...」以降にコントローラに渡っているパラメータが書いてあるので、どうなっているか教えていただきたいです。
下でindexを開いた時のログを載せているように、送信ボタンを押した時のログも載せてもらえますか?
logはこんな感じになっています。
Started POST "/accepted/26/accepted" for ::1 at 2019-12-16 18:22:11 +0900
Processing by AcceptController#acceptednew as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"4HcoYAC4TXuI98wKcZHXhdlvcKEIBTwde96NLW2yIyOV4FeQ3Job44vadhydwE45oMG6paM/KBkHBoI33PnWmg==", "post_id"=>"2", "id"=>"26"}
User Load (1.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 26], ["LIMIT", 1]]
↳ app/controllers/application_controller.rb:6
Post Load (10.6ms) SELECT "posts".* FROM "posts" WHERE "posts"."id" = ? LIMIT ? [["id", 2], ["LIMIT", 1]]
↳ app/controllers/accept_controller.rb:8
(0.1ms) begin transaction
↳ app/controllers/accept_controller.rb:13
Accept Create (13.3ms) INSERT INTO "accepts" ("user_id", "post_id", "created_at", "updated_at") VALUES (?, ?, ?, ?) [["user_id", 26], ["post_id", 2], ["created_at", "2019-12-16 18:22:12.653896"], ["updated_at", "2019-12-16 18:22:12.653896"]]
↳ app/controllers/accept_controller.rb:13
(124.3ms) commit transaction
↳ app/controllers/accept_controller.rb:13
Redirected to http://localhost:3000/users/26
Completed 302 Found in 991ms (ActiveRecord: 151.5ms)
Started GET "/users/26" for ::1 at 2019-12-16 18:22:13 +0900
Processing by UsersController#show as HTML
Parameters: {"id"=>"26"}
User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 26], ["LIMIT", 1]]
↳ app/controllers/application_controller.rb:6
User Load (0.5ms) SELECT "users".* FROM "users" WHERE "users"."id" IS NULL LIMIT ? [["LIMIT", 1]]
↳ app/controllers/users_controller.rb:30
(0.5ms) SELECT COUNT(*) FROM "accepts" WHERE "accepts"."user_id" = ? [["user_id", 26]]
↳ app/controllers/users_controller.rb:31
(1.2ms) SELECT COUNT(*) FROM "posts" WHERE "posts"."user_id" = ? [["user_id", 26]]
↳ app/controllers/users_controller.rb:32
Rendering users/show.html.erb within layouts/application
Rendered users/show.html.erb within layouts/application (7.8ms)
Completed 200 OK in 466ms (Views: 280.4ms | ActiveRecord: 2.6ms)
Started GET "/users/users.scss" for ::1 at 2019-12-16 18:22:14 +0900
Started GET "/users/home-mobile.scss" for ::1 at 2019-12-16 18:22:14 +0900
Started GET "/users/home.scss" for ::1 at 2019-12-16 18:22:14 +0900
Processing by UsersController#show as
Parameters: {"id"=>"users"}
User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 26], ["LIMIT", 1]]
Processing by UsersController#show as
↳ app/controllers/application_controller.rb:6
Parameters: {"id"=>"home-mobile"}
User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."id" IS NULL LIMIT ? [["LIMIT", 1]]
User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 26], ["LIMIT", 1]]
↳ app/controllers/users_controller.rb:30
↳ app/controllers/application_controller.rb:6
User Load (1.1ms) SELECT "users".* FROM "users" WHERE "users"."id" IS NULL LIMIT ? [["LIMIT", 1]]
↳ app/controllers/users_controller.rb:30
(0.4ms) SELECT COUNT(*) FROM "accepts" WHERE "accepts"."user_id" = ? [["user_id", 26]]
(0.5ms) SELECT COUNT(*) FROM "accepts" WHERE "accepts"."user_id" = ? [["user_id", 26]]
↳ app/controllers/users_controller.rb:31
↳ app/controllers/users_controller.rb:31
(1.1ms) SELECT COUNT(*) FROM "posts" WHERE "posts"."user_id" = ? [["user_id", 26]]
↳ app/controllers/users_controller.rb:32
(1.1ms) SELECT COUNT(*) FROM "posts" WHERE "posts"."user_id" = ? [["user_id", 26]]
Processing by UsersController#show as
↳ app/controllers/users_controller.rb:32
Parameters: {"id"=>"home"}
Rendering users/show.html.erb within layouts/application
User Load (0.6ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 26], ["LIMIT", 1]]
Rendered users/show.html.erb within layouts/application (3.7ms)
↳ app/controllers/application_controller.rb:6
Rendering users/show.html.erb within layouts/application
User Load (1.1ms) SELECT "users".* FROM "users" WHERE "users"."id" IS NULL LIMIT ? [["LIMIT", 1]]
↳ app/controllers/users_controller.rb:30
Rendered users/show.html.erb within layouts/application (0.7ms)
(0.5ms) SELECT COUNT(*) FROM "accepts" WHERE "accepts"."user_id" = ? [["user_id", 26]]
↳ app/controllers/users_controller.rb:31
(0.5ms) SELECT COUNT(*) FROM "posts" WHERE "posts"."user_id" = ? [["user_id", 26]]
↳ app/controllers/users_controller.rb:32
Rendering users/show.html.erb within layouts/application
Rendered users/show.html.erb within layouts/application (0.3ms)
Completed 200 OK in 1215ms (Views: 736.9ms | ActiveRecord: 3.0ms)
Completed 200 OK in 1655ms (Views: 712.8ms | ActiveRecord: 2.4ms)
Started GET "/users/home-mobile.scss" for ::1 at 2019-12-16 18:22:16 +0900
Completed 200 OK in 954ms (Views: 448.0ms | ActiveRecord: 2.7ms)
Processing by UsersController#show as
Parameters: {"id"=>"home-mobile"}
User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 26], ["LIMIT", 1]]
↳ app/controllers/application_controller.rb:6
User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" IS NULL LIMIT ? [["LIMIT", 1]]
↳ app/controllers/users_controller.rb:30
(0.5ms) SELECT COUNT(*) FROM "accepts" WHERE "accepts"."user_id" = ? [["user_id", 26]]
↳ app/controllers/users_controller.rb:31
(1.0ms) SELECT COUNT(*) FROM "posts" WHERE "posts"."user_id" = ? [["user_id", 26]]
↳ app/controllers/users_controller.rb:32
Rendering users/show.html.erb within layouts/application
Rendered users/show.html.erb within layouts/application (1.5ms)
Completed 200 OK in 363ms (Views: 168.4ms | ActiveRecord: 2.1ms)
Started GET "/users/home.scss" for ::1 at 2019-12-16 18:22:17 +0900
Processing by UsersController#show as
Parameters: {"id"=>"home"}
User Load (0.7ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 26], ["LIMIT", 1]]
↳ app/controllers/application_controller.rb:6
User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" IS NULL LIMIT ? [["LIMIT", 1]]
↳ app/controllers/users_controller.rb:30
(0.4ms) SELECT COUNT(*) FROM "accepts" WHERE "accepts"."user_id" = ? [["user_id", 26]]
↳ app/controllers/users_controller.rb:31
(0.4ms) SELECT COUNT(*) FROM "posts" WHERE "posts"."user_id" = ? [["user_id", 26]]
↳ app/controllers/users_controller.rb:32
Rendering users/show.html.erb within layouts/application
Rendered users/show.html.erb within layouts/application (2.4ms)
Completed 200 OK in 288ms (Views: 186.0ms | ActiveRecord: 1.8ms)
ありがとうございました。
<% @posts.each do |post| %>
<%= form_tag("/accepted/#{@current_user.id}/accepted",method: :post,data:{confirm:"このクエストを受けますか?"}) do %>
ループの中にform_tagを書いたら出来ました。
いろいろ考えてくださってありがとうございます。
確かにその通りですね…!
解決してよかったです!
回答1件
あなたの回答
tips
プレビュー