我是ruby on rails的新手。 我试图发送选择选项选择的数据,但在创建动作ActionController :: InvalidAuthenticityToken被引发,我无法弄清楚原因。 如果我从表单中删除collection_select行,则表单提交。
应用程序/视图/代码/ new.html.erb
<%= form_for @code, url: {action: "create"}, html: {class: "form-horizontal form-bordered"} do |f| %>
<div class="form-body">
<div class="form-group TextBoxContainer">
<label class="control-label col-md-2"> Tracking Code(s) </label>
<div class="col-md-6">
<%= f.text_field :code, name: "[code][]" , class: "form-control",placeholder: "Enter Tracking Code(s) for Analytics" %>
</div>
<div class="actions col-md-4 div_tag">
<button type="button" class="btn dark btn-outline add_another_code">
<i class="fa fa-plus"></i> Add Another Code</button>
</div>
</div>
</div>
<!-- <%= f.collection_select(:webpage_id,Webpage.all, :id, :title,{prompt: true})%> -->
<%= f.button :submit, class: 'btn red btn-lg' %>
<% end %>
这是我的codes_controller.rb
应用程序/资产/控制器/ codes_controller.rb
class CodesController < ApplicationController
def new
@code = Code.new
end
def create
debugger
@code = Code.new
@code.webpage_id = params[:webpage_id]
params[:code].each do |code|
@code.code << code
end
if @code.save
redirect_to '/'
else
render 'new'
end
end
def edit
@code = Code.find(params[:id])
end
def update
@code = Code.find(params[:id])
if @code.update(code_params)
redirect_to '/'
else
render 'edit'
end
end
private
def code_params
params.require(:code).permit(:webpage_id,code:[])
end
end