chandradot99
chandradot99

Reputation: 3866

How to use parameters in rails controller

Below are my parameters passed to a rails controller

Parameters: {"file"=>#<ActionDispatch::Http::UploadedFile:0x007f6244989100 @tempfile=#<Tempfile:/tmp/RackMultipart20151109-3635-1y6e4wr.mp3>,
@original_filename="07 - Barfi - Ala Barfi! (Version 2) [DM].mp3",
@content_type="audio/mp3", @headers="Content-Disposition: form-data; name=\"file\"; filename=\"07 - Barfi - Ala Barfi! (Version 2) [DM].mp3\"\r\nContent-Type: audio/mp3\r\n">, 
"event_id"=>"19"}

then i am storing it in my database

  def create
    event = Event.find(params[:event_id])
    playlist = event.playlists.create(file: playlist_params[:file], name: playlist_params[:@original_filename])
    render json: event
  end

  private

  def playlist_params
    params.permit(:file, :@original_filename)
  end

below is my schema of playlist model

  create_table "playlists", force: :cascade do |t|
    t.string   "file",       limit: 255
    t.integer  "event_id",   limit: 4
    t.datetime "created_at",             null: false
    t.datetime "updated_at",             null: false
    t.string   "name",       limit: 255
  end

but my name column is showing null.

below is the server side code for above

Started POST "/events/19/playlists" for 127.0.0.1 at 2015-11-09 18:06:17 +0530

Processing by Api::V1::PlaylistsController#create as JSON

Parameters: {"file"=>#<ActionDispatch::Http::UploadedFile:0x007f6244989100 @tempfile=#<Tempfile:/tmp/RackMultipart20151109-3635-1y6e4wr.mp3>, @original_filename="07 - Barfi - Ala Barfi! (Version 2) [DM].mp3", @content_type="audio/mp3", @headers="Content-Disposition: form-data; name=\"file\"; filename=\"07 - Barfi - Ala Barfi! (Version 2) [DM].mp3\"\r\nContent-Type: audio/mp3\r\n">, "event_id"=>"19"}

Can't verify CSRF token authenticity

Event Load (0.2ms)  SELECT  `events`.* FROM `events` WHERE `events`.`id` = 19 LIMIT 1
Unpermitted parameters: format, event_id
Unpermitted parameters: format, event_id

(0.2ms)  BEGIN
SQL (0.4ms)  INSERT INTO `playlists` (`file`, `event_id`, `created_at`, `updated_at`) VALUES ('07_-_Barfi_-_Ala_Barfi___Version_2___DM_.mp3', 19, '2015-11-09 12:36:17', '2015-11-09 12:36:17')
   (55.3ms)  COMMIT

Completed 200 OK in 161ms (Views: 1.4ms | ActiveRecord: 60.3ms)

I don't know why sql is not inserting in name column in playlist table in above server side code.

Upvotes: 2

Views: 202

Answers (1)

Kyle
Kyle

Reputation: 1058

The file attribute that is being passed in the params actually has most of the data that you need. @original_filename is an instance variable of the file that is being uploaded.

You could try playing with the strong_parameters portion of your controller and try modifying it to look like this:

def playlist_params
  params.permit(file: [])
end

Then when you go to save the file, you can change your code to look like this instead:

def create
  event = Event.find(params[:event_id])
  file = playlist_params[:file]
  playlist = event.playlists.create(file: file, name: file.original_filename)
  render json: event
end

You may go as far as saying you don't really need the playlist_params and you could just access params[:file] like this:

def create
  event = Event.find(params[:event_id])
  file = params[:file]
  playlist = event.playlists.create(file: file, name: file.original_filename)
  render json: event
end

Upvotes: 1

Related Questions