如何在编辑页面中显示:user_id
来样者?
edit.html.erb
<%= simple_form_for(@duel) do |f| %>
<%= @dueler.user_id %>
<% end %>
rails c
Duel.last
id: 11,
consequence: "bad",
reward: "good",
Dueler.find(15)
id: 15,
user_id: 78, # I want to get this ID
challenge_id: 179,
duel_id: 11,
Dueler.last
id: 16,
user_id: 15,
challenge_id: 190,
duel_id: 11,
duels_controller
@dueler = Dueler.find(params[:user_id]) # THIS GIVES ERROR: ActiveRecord::RecordNotFound in DuelsController#edit Couldn't find Dueler with 'id'=
FULL duels_controller
class DuelsController < ApplicationController
before_action :set_duel, only: [:show, :edit, :update, :destroy]
respond_to :html
def index
@duels = Duel.joins(:duelers).all
respond_with(@duels)
end
def show
respond_with(@duel)
end
def new
@duel = Duel.new
respond_with(@duel)
end
def edit
@dueler = Dueler.find_by(user_id: params[:dueler][:user_id]) # Sri Vishnu Totakura's Suggested Change
end
def create
@duel = Duel.new(duel_params)
@duel.save
respond_with(@duel)
end
def update
@duel.update(duel_params)
respond_with(@duel)
end
def destroy
@duel.destroy
respond_with(@duel)
end
private
def set_duel
@duel = Duel.find(params[:id])
end
def duel_params
params.require(:duel).permit(:consequence, :reward, duelers_attributes: [:id, :user_id, :challenge_id, :accept])
end
end