如何在rails控制器中使用参数 [英] How to use parameters in rails controller

查看:39
本文介绍了如何在rails控制器中使用参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我传递给 rails 控制器的参数

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)

我不知道为什么 sql 在上面的服务器端代码中没有插入播放列表表的名称列中.

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

推荐答案

在 params 中传递的文件属性实际上包含您需要的大部分数据.@original_filename 是正在上传的文件的实例变量.

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.

您可以尝试使用控制器的 strong_parameters 部分并尝试将其修改为如下所示:

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

你可能会说你真的不需要 playlist_params,你可以像这样访问 params[:file] :

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

这篇关于如何在rails控制器中使用参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆