未授权的参数::utf8、:authenticity_token - Rails 5.2 form_with

3
我正在为这个问题苦恼。在使用嵌套资源的form_with表单中,我遇到了不允许的参数错误。我使用的是Rails 5.2.1和Ruby 2.5。
我不确定自己错在哪里。我尝试过各种site_params的变化,但都没有成功。如果能得到任何帮助将不胜感激。
下面是我的routes.rb:
resources :locations do
    post 'sites', to: 'sites#custom_create', as: :site_custom
    resources :sites, except: [:edit, :update, :show]
  end

相关的控制器函数:

  def new 
    verify_site_name or return
    @site =  @location.sites.new
    authorize @site
    @available_site = AvailableSite.find_by(site_name: params[:site_name])
    @finder_results = get_finder_results([:site_name], @location)
  end

  def create
    verify_site_name or return
    @site = @location.sites.new(site_params)
    authorize @site
    respond_to do |format|
      if @site.save
        format.html { redirect_to location_sites_path, notice: 'Location was successfully created.' }
        format.json { render :show, status: :created, site: @site }
      else
        format.html { redirect_to location_sites_path, alert: "#{@site.errors.full_messages.first}" }
        format.json { render json: @site.errors, status: :unprocessable_entity }
      end
    end
  end


# Never trust parameters from the scary internet, only allow the white list through.
    def site_params
      params.permit(:location_id, :place_id, :site_name, :review_url)
    end
    # Use callbacks to share common setup or constraints between actions.
    def set_site
      @site = Site.find(params[:id])
    end
    def set_location
      @location = Location.friendly.find(params[:location_id])
    end

当然,还有表单本身:
<%= form_with(model: [@location, @site], local: true, class: 'site-form') do |form| %>
      <%= hidden_field_tag(:site_name, @available_site.site_name) %>
      <div class="field md:w-3/4 lg:w-2/3 mx-auto text-left">
        <%= form.text_field :review_url, class: 'text-input',  placeholder: 'https://www.facebook.com/yourbusinessname/review/?ref=page_internal'  %>
        <span class="form-required">*required</span>
      </div>
      <%= form.submit "Manually Submit #{@available_site.site_name.titleize}", class: 'btn btn-green btn-outline' %>
    <% end %>

最后是日志:
Started POST "/locations/tekamar-mortgages-ltd/sites" for 127.0.0.1 at 2018-12-03 15:30:57 +0000
Processing by SitesController#custom_create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"l/DjkUbVNyw+nrXxo1B/9IGru043Ftroxy8FcuNcZuxmJ7V3j0gC8njm5kpGPT8c7tMWSaAR/ler3cSHY+t8aA==", "site"=>{"site_name"=>"google", "review_url"=>"https://www.yelp.ca/biz/your-busines-sname?utm_campaign=www_business_share_popup&utm_medium=copy_link&utm_source=(direct)"}, "commit"=>"Create Site", "location_id"=>"tekamar-mortgages-ltd"}
  Location Load (0.8ms)  SELECT  "locations".* FROM "locations" WHERE "locations"."slug" = $1 LIMIT $2  [["slug", "tekamar-mortgages-ltd"], ["LIMIT", 1]]
  ↳ app/controllers/sites_controller.rb:78
  User Load (1.9ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2  [["id", 1], ["LIMIT", 1]]
  ↳ /Users/richsmith/.rvm/gems/ruby-2.5.1/gems/activerecord-5.2.1/lib/active_record/log_subscriber.rb:98
Unpermitted parameters: :utf8, :authenticity_token, :site, :commit
Redirected to http://localhost:3000/locations/tekamar-mortgages-ltd/sites
Completed 302 Found in 13ms (ActiveRecord: 2.6ms)
1个回答

1

尝试:

def site_params
  params.require(:site).permit(:location_id, :place_id, :site_name, :review_url)
end

site的参数嵌套在params[:site]中。你应该先从所有参数中取出这个哈希,然后对其调用permit。现在你正在对所有参数进行消毒处理(包括一些明显不感兴趣的东西,如utf8authenticity_token)。


我之前尝试过,但不幸的是,这只让我无法呈现“新”路径。我收到了错误消息:参数丢失或值为空:站点。我认为这是由于在New Controller Action中调用params函数所致(请参见上面的问题)。有什么想法吗? - Rich
1
为什么在#new操作中需要site_params?通常情况下,应该调用AvailableSite.new - mrzasa
啊,我尝试着将 site_params[:site_name] 改为直接调用 params[:site_name]。这个变量在视图中被使用。 - Rich
但是为什么你需要在 new 中使用那些参数呢? - mrzasa
因为它们用于确定新视图。 - Rich

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接