爲什麼我在提交表單時出現此錯誤?我不是已經建立了ID嗎?提交form_for時路由參數不可用?
ActiveRecord::RecordNotFound at /polls/:poll_id/responses
Couldn't find Poll with 'id'=
在我的Polls_Controller
我讓所有表單輸入訪問。 poll_id
來自路由參數,顯示路由成功響應:id
。
def show
@poll_options = @poll.options
# since you can respond to a poll from its show route
@poll_response = PollResponse.new
end
def responses
@poll_response = PollResponse.create(
params.require(:poll_response).permit(:poll_option_id, :ip_address, :poll_id)
)
if @poll_response.save
redirect_to "/welcome"
else
end
end
private
def set_poll
@poll = Poll.find(params[:id])
end
我的節目視圖成功加載在/polls/:id
,但是,當我提交表單,它看起來像Rails的將無法訪問該PARAM因爲路由變化。
#Poll/Show.html.erb
<%= form_for(@poll_response, url: '/polls/:poll_id/responses/') do |f| %>
<%= f.hidden_field(:ip_address, value: request.remote_ip) %>
<%= f.hidden_field(:poll_id, value: @poll.id) %>
<ul>
<% @poll_options.each do |option_id, option_value| %>
<li>
<%= f.radio_button(:poll_option_id, option_id) %>
<%= option_value %> </br>
</li>
<% end %>
</ul>
<%= f.submit "Vote!" %>
<% end %>
這是我的路線。 Poll_Response和Poll_Option模型屬於Poll。
resources :polls, only: [:show, :create, :index, :responses] do
post 'responses', as: :responses
end
你在哪裏調用'set_poll'?也許'before_action set_poll,只有:[:show]' – rogelio