我一直在創建一個系統,我創建這些條目,但問題是當我試圖創建編輯功能,它不更新帖子,但創建一個新的。更新功能不更新帖子,但創建一個新帖子
我控制器文件看起來像這樣:
class BetsController < ApplicationController
def new
@bet = Bet.new
end
def create
@bets = Bet.new(bet_params)
if @bets.save
flash[:success] = 'Bet Successfull Logged.'
redirect_to new_bet_path
else
flash[:danger] = 'Error, Bet has not been logged. Try again mate.'
redirect_to new_bet_path
end
end
def show
@bet = Bet.find(params[:id])
end
def edit
@bet = Bet.find(params[:id])
end
def update
@bet = Bet.find(params[:id])
if @bet.update_attributes(bet_params)
flash[:success] = "Bet Updated!"
redirect_to bet_path(params[:id])
else
render action: :edit
end
end
private
def bet_params
params.require(:bet).permit(:bet_placed, :game, :units_placed, :odds, :profit_or_loss)
end
end
而且也被提交看起來像這樣的形式:
<%= form_for :bet, url: bets_path, :html => { :multipart => true } do |f| %>
<p class="form-group">
<%= f.label :bet_placed %><br>
<%= f.text_field :bet_placed, class: 'form-control' %>
</p>
<p class="form-group">
<%= f.label :game %><br>
<%= f.text_field :game, class: 'form-control' %>
</p>
<p class="form-group">
<%= f.label :units_placed %><br>
<%= f.text_field :units_placed, class: 'form-control' %>
</p>
<p class="form-group">
<%= f.label :odds %><br>
<%= f.text_field :odds, class: 'form-control' %>
</p>
<p class="form-group">
<%= f.label :profit_or_loss %><br>
<%= f.text_field :profit_or_loss, class: 'form-control' %>
</p>
<%= f.submit 'Update Profile', class: 'btn btn-default' %>
<% end %>