2016-04-24 157 views
-4

我一直在試圖解決以下錯誤幾乎8小時以上如何在ReservationsController#create中修復ActionController :: ParameterMissing?

Param is missing or the value is empty: reservations 

Application Trace | Framework Trace | Full Trace 

app/controllers/reservations_controller.rb:18:in `reservations_params' 
app/controllers/reservations_controller.rb:11:in `create' 

有人可以幫我解決這個問題?我的代碼如下。

new.html.rb源:

<%= form_for @reservations do |f| %> 
    <%= f.label :table_number %><p &nbsp;> 
    <select name="table_number"> 
     <option>1</option> 
     <option>2</option> 
     <option>3</option> 
     <option>4</option> 
     <option>5</option> 
     <option>6</option> 
     <option>7</option> 
     <option>8</option> 
     <option>9</option> 
     <option>10</option> 
     <option>11</option> 
     <option>12</option> 
    </select><br><br> 
    <label>Number of Seats:</label><p &nbsp;> 
    <%= f.text_field :num_seats %><br><br> 
    <label>Number of Guest:</label><p &nbsp;></p> 
    <%= f.text_field :guest_size %><br><br> 
    <label>Reservation Time </label><p &nbsp;> 
    <%= f.time_select :requested_time %><br><br> <!-- ,{ampm: true} --> 
    <%= f.submit class: "rc_btn_02" %><br><br> 
<% end %> 

reservations_controller.rb源:

def index 
    @reservations = Reservation.new 
end 

def new 
    @reservations = Reservation.new 
end 
def create 
    @reservations = Reservation.new(reservations_params) 
    @reservations.save 
    redirect_to @reservations 
end 


def reservations_params 
     params.require(:reservations).permit(:table_number,:num_seats,:guest_size,:requested_date,:requested_time) 
end 

錯誤消息:

This is the error message.

+0

@Alfie我不認爲這是一個重複的問題。錯誤的原因是不同的。還有解決方案。 – psantos

+0

錯誤是一樣的('ActionController :: ParameterMissing')。只是參數名稱不同。 – Alfie

+0

查看提供的解決方案中的評論,你會明白 – Alfie

回答

2

reservations_params的型號名稱應該是單數:params.require(:reservation)...

改變這樣的代碼:

def reservations_params 
    params.require(:reservations).permit(:table_number,:num_seats,:guest_size,:requested_date,:requested_time) 
end 

到:

def reservations_params 
    params.require(:reservation).permit(:table_number,:num_seats,:guest_size,:requested_date,:requested_time) 
end 

如果你看到的參數列表顯示錯誤信息,你會看到有一個reservation鍵,但不是reservations

+0

好的。我會盡力 :)... – Cross

相關問題