为什么我在提交表单时出现此错误?我不是已经建立了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