[英]ActionController::InvalidAuthenticityToken when i am using collection_select in form_for tag
i am new to ruby on rails. 我是红宝石的新手。 i am trying to send data selected by select option, but on create action ActionController::InvalidAuthenticityToken is raised and i can't figure out why.
我正在尝试发送通过选择选项选择的数据,但是在创建操作时引发了ActionController :: InvalidAuthenticityToken,我不知道为什么。 if i remove collection_select line from form then form submits.
如果我从表单中删除collection_select行,则表单提交。
app/views/codes/new.html.erb app / views / codes / 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 %>
this is my codes_controller.rb 这是我的codes_controller.rb
app/assets/controller/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
试试这个:
<%= f.select :webpage_id, Webpage.all.map {|c| [c.title, c.id] }, {prompt:"Choose Webpage"} %>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.