简体   繁体   中英

ActionController::Parameters duplicated key

The following snippet is from one of my controllers:

Class Admin::Portfolios::PortfoliosController < Comfy::Admin::Cms::BaseControllers

  def create
    uploader = PortfolioUploader.new
    uploader.store!(params[:portfolio][:thumbnail])

    @portfolio.save!
    flash[:notice] = 'Portfolio item saved!'
    redirect_to :action => :index
  rescue ActiveRecord::RecordInvalid
    flash.now[:error] = 'Error saving portfolio item'
    render :action => :new
  end

In development mode params[:portfolio][:thumbnail] is set in the controllers request params. However in production mode params[:portfolio_portfolio][:thumbnail] is set instead.

I'm using form_for(@portfolio) and in development all the form fields name are portfolio[name] and portfolio_portfolio[name] in production mode. See the erb below.

<%= form_for @portfolio, :url => {:action => :create}, :html => {:multipart => true} do |form| %>
    <% render :partial => form %>
<% end %>

Can anybody explain why this might be happening?

The issue is with my application structure

app
├── controllers
├── helpers
├── mailers
├── models
│   ├── portfolio
│   │   └── portfolio.rb
│   ├── portfolio.rb
└──

form_form looks for an :as option to use as the form and field names. If no :as option is passed it will execute model_name_from_record_or_class which does what it says on the tin. The portfolio.rb class higher up in the model directory shouldn't have existed and was an empty class.

Because the Portfolio class was already defined the class Portfolio::Portfolio was transformed to portfolio_portfolio .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM