Rails parameters are not passed to the 'params' variable

I am making an application using Rails 4.

Basically, I have two models: Card and CardCollection. CardCollections has_many Cards. I applied this relationship with nested attributes.

class Card < ActiveRecord::Base
  belongs_to :card_collection
end

class CardCollection < ActiveRecord::Base
  has_many :uploaded_cards, class_name: 'Card', dependent: :destroy
  accepts_nested_attributes_for :uploaded_cards, allow_destroy: true
end

And the shape as below.

<%= form_for @card_collection, options do |f| %>
  <%= f.fields_for :uploaded_cards do |ff| %>
    <%= render 'cards/card_form', f: ff %>
  <% end %>
<% end %>

When I put some data on the form and click the submit button, I see that the parameters are passed correctly to the server. (According to server log)

Parameters: {"utf8"=>"✓", "authenticity_token"=>"YFn7RsQzR4Sq1bjR+mOhCASWmeqAxw9B30oAHCL
9qzk=", "card_collection"=>{"collectible_id"=>"16", "collectible_type"=>"Scrapbook", "uplo
aded_cards_attributes"=>{"0"=>{"_destroy"=>"false", "image_url"=>"https://s3-ap-northeast-
1.amazonaws.com/bucketplace/uploads/image_content/src/2833/2b67142422.jpg", "place"=>"1",
"style"=>"0", "description"=>"생각을 짓다......", "id"=>"97"}, "1"=>{"image_url"=>"http://
hodoli.s3.amazonaws.com/uploads%2F14_06_02_17_17_56_62276930_468703135.jpg", "place"=>"7",
 "style"=>"1", "description"=>"123"}}}, "project_cover_image_url"=>"", "commit"=>"확인", "
id"=>"16"}

But when I actually print the params variable in the program code, the params [: card_collection] [: uploaded_cards_attributes] hash parameter (part of the nested attributes) is unexpectedly missing. Therefore, the value of the printed parameters is as follows.

"params=  {\"utf8\"=>\"✓\", \"_method\"=>\"patch\", \"authenticity_token\"=>\"YFn7RsQzR4Sq
1bjR+mOhCASWmeqAxw9B30oAHCL9qzk=\", \"card_collection\"=>{\"collectible_id\"=>16, \"collec
tible_type\"=>\"Scrapbook\"}, \"project_cover_image_url\"=>\"\", \"commit\"=>\"확인\", \"a
ction\"=>\"update\", \"controller\"=>\"card_collections\", \"id\"=>\"16\"}"
Unpermitted parameters: collectible_id, collectible_type

, . , " ", Rails. ( ) , .

class CardCollectionsController < ApplicationController
before_action :set_card_collection, only: [:do_upload, :edit, :update]
before_action :authenticate_user!, only: [:upload, :do_upload, :edit, :update]
before_action :authenticate_card_collection_owner!, only: [:do_upload, :edit, :update]

# GET /card_collections/upload_select
def upload_select
end

# GET /card_collections/upload
def upload
    @card_collection = CardCollection.new
end

def do_upload
    respond_to do |format|
        p card_collection_params
        if @card_collection.update(card_collection_params)
            format.html do
                @card_collection.collectible.update(cover_image_url: params[:project_cover_image_url]) unless params[:project_cover_image_url].blank?
                redirect_to @card_collection.collectible, notice: 'CardCollection was successfully updated.'
            end
            format.json { head :no_content }
        else
            @card_collection = CardCollection.new(card_collection_params)
            format.html { render 'upload', alert: '에러' }
            format.json { render json: @card_collection.errors, status: :unprocessable_entity }
        end
    end
end

# GET /card_collections/1/edit
def edit
end

# PATCH/PUT /card_collections/1
# PATCH/PUT /card_collections/1.json
def update
    respond_to do |format|
        if @card_collection.update!(card_collection_params)
            format.html do
                @card_collection.collectible.update(cover_image_url: params[:project_cover_image_url]) unless params[:project_cover_image_url].blank?
                redirect_to @card_collection.collectible, notice: 'CardCollection was successfully updated.'
            end
            format.json { head :no_content }
        else
            format.html { render 'edit', alert: '에러' }
            format.json { render json: @card_collection.errors, status: :unprocessable_entity }
        end
    end
end

private

# Use callbacks to share common setup or constraints between actions.
def set_card_collection
    if params[:id]
        @card_collection = CardCollection.find(params[:id])
        params[:card_collection] = {collectible_id: @card_collection.collectible_id, collectible_type: @card_collection.collectible_type}
    elsif params[:card_collection][:collectible_id].blank?
        @card_collection = CardCollection.new(card_collection_params)
        flash.now[:alert] = '스크랩북을 선택해 주세요'
        render 'upload'
    else
        @card_collection = CardCollection.find_by_collectible_id_and_collectible_type(params[:card_collection][:collectible_id], params[:card_collection][:collectible_type])
    end
end

# Never trust parameters from the scary internet, only allow the white list through.
def card_collection_params
    params.require(:card_collection).permit(CardCollection.param_names + [uploaded_cards_attributes: Card.param_names])
end

def authenticate_card_collection_owner!
    redirect_to previous_url if current_user != @card_collection.user
end
end

class CardCollection < ActiveRecord::Base
def self.param_names
    [:id, :name, :description]
end
end

class Card < ActiveRecord::Base
    def self.param_names
    [:id, :image_url, :description, :place, :style, :source, :width, :height]
end
end

, @Rich Peck. Rails. , set_card_collection.

resources :card_collection, only: [:edit, :update] do
  get :upload, on: :collection
  patch :do_upload, on: :collection 
end

route.rb . ( CardCollection, ), , CardCollection . , /card _collection/1/edit, Rails.

set_card_collection .

if params[:id]
        @card_collection = CardCollection.find(params[:id])
        params[:card_collection] = {collectible_id: @card_collection.collectible_id, collectible_type: @card_collection.collectible_type}
    elsif params[:card_collection][:collectible_id].blank?
        @card_collection = CardCollection.new(card_collection_params)
        flash.now[:alert] = '스크랩북을 선택해 주세요'
        render 'upload'
    else
        @card_collection = CardCollection.find_by_collectible_id_and_collectible_type(params[:card_collection][:collectible_id], params[:card_collection][:collectible_type])

, EXACT, : : , , . , route.rb, , , .

resources :card_collections, only: [] do
    get :upload_select, on: :collection
    get :upload, on: :collection
    post :upload, on: :collection, to: :do_upload
    get :edit, on: :collection
    patch :update, on: :collection
    put :update, on: :collection
end
+4
2

,

-

, uploaded_cards_attributes,

, strong params ( , params ( , ), '

, , Rails

-

,

, edit/update ( PATCH). , Rails .

new - , . , nested resources ​​ CardCollection:

#config/routes.rb
resources :card_collections do
   resources :card_uploads
end

- :

#app/controllers/card_collections_controller.rb
class CardCollectionsController < AppicationController
   def edit
       @card_collection = CardCollection.find params[:card_collection_id]
       @card_upload = @card_collectiom.new #-> might need to change
   end
end

#app/views/card_collections/edit.html.erb
<%= form_for @card_upload do |f| %>
    <%=  f.file_field :image %>
    <%=  f.submit %>
<% end %>

, , ; , params.

-

btw attribute_names, strong params :

def your_params
    parameters = Model.attribute_names - %w(id created_at updated_at)
    params.require(:parent).permit(parameters)
end
+1

MIME, Rails. Rails , , "application/vnd.api + json".

config/initializers/mime_types.rb:

Mime::Type.register "application/vnd.api+json", :json

. Https://github.com/rails/rails/issues/28564 .

0

Source: https://habr.com/ru/post/1544059/


All Articles