在我的項目中,我嘗試更新記錄,所以一旦我點擊「更新」我被重定向到前一頁,就好像該記錄自更新以來參數通過。但是,porduction.log不是更新,而是顯示302重定向的返回,但沒有訪問數據庫來更改參數中的值。SQLite數據庫不更新記錄與PUT-返回一個302重定向rails
控制器:
def show
@hour = HourLog.find(params[:id])
@status = @hour.status
end
def update
@hour = HourLog.find(params[:id])
if @hour.update(update_hour_params)
redirect_to '/hours'
else
redirect_to "/hours/#{id}"
end
end
視圖(show.html.erb):
<div class="table-responsive">
<%= form_for(@hour, url: hour_path, method: :patch) do |f| %>
<table class="table table-striped">
<thead>
<tr id="dashtexttable">
<th>User</th>
<th>Assignment</th>
<th>Hours</th>
<th>Supervisor/Location</th>
<th>Date</th>
<th>Status</th>
<th></th>
</tr>
</thead>
<tbody>
<tr id="dashfield">
<td><%= @hour.user.first_name + ' ' + @hour.user.last_name %></td>
<td><%= f.text_field :assignment, :value => @hour.assignment %></td>
<td><%= f.number_field :hours, in: 0.5..12.0, step: 0.5, :value => @hour.hours %></td>
<td> <%= f.text_field :supervisor, :value => @hour.supervisor %></td>
<td><%= f.date_field :date, :value => @hour.date %></td>
<td><%= f.hidden_field :status, :value => 'Confirmed'%>
<%= f.submit 'Update'%></td>
<%end %>
<td><%= link_to 'Delete', hour_path(@hour), method: :delete %></td>
</tr>
</tbody>
</table>
</div>
途徑:
root 'welcome#index'
get '/signup' => 'users#new'
resources :users
get '/login' => 'sessions#new'
post '/login' => 'sessions#create'
delete '/logout' => 'sessions#destroy'
post '/hours/new' => 'hours#create'
resources :hours
get '/leaderboard' => 'leaderboard#index'
get '/essay' => 'essay#index'
resources :essay
模型
class HourLog < ActiveRecord::Base
belongs_to :user
end
任何幫助將是偉大的,謝謝!
提供您的模型。更新可能會失敗並重定向到「/ hours /#{id}」,因爲某些內容在您的更新(您的模型類)上無效。 –