Skip to content

Commit b186a95

Browse files
authored
Merge pull request #43 from rootstrap/support-parameters
Support ActionController::Parameters
2 parents 4b9d2e6 + 5d73956 commit b186a95

File tree

3 files changed

+659
-176
lines changed

3 files changed

+659
-176
lines changed

README.md

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ class User < ActiveRecord::Base
5959
end
6060
```
6161

62-
on your controller you could do something like this:
62+
on your controller you could do any of the following:
6363
```ruby
6464
class UsersController < ApplicationController
6565
def create
@@ -69,17 +69,16 @@ class UsersController < ApplicationController
6969
private
7070

7171
def user_params
72-
params.require(:user).permit(avatar: [:data], :username, :email)
72+
params.require(:user).permit(avatar: :data, :username, :email)
7373
end
7474
end
7575
```
7676

77-
Or you could also do:
7877
```ruby
7978
class UsersController < ApplicationController
8079
def create
8180
user = User.create(user_params)
82-
user.avatar.attach(params[:avatar])
81+
user.avatar.attach(data: params[:avatar]) # params[:avatar] => 'data:image/png;base64,[base64 data]'
8382
end
8483

8584
private
@@ -90,12 +89,30 @@ class UsersController < ApplicationController
9089
end
9190
```
9291

93-
Here's another option to achieve the same:
9492
```ruby
9593
class UsersController < ApplicationController
9694
def create
9795
user = User.create(user_params)
98-
user.avatar = { data: params[:avatar] }
96+
user.avatar.attach(avatar_params) # avatar_params => { data: 'data:image/png;base64,[base64 data]' }
97+
end
98+
99+
private
100+
101+
def user_params
102+
params.require(:user).permit(:username, :email)
103+
end
104+
105+
def avatar_params
106+
params.require(:avatar).permit(:data)
107+
end
108+
end
109+
```
110+
111+
```ruby
112+
class UsersController < ApplicationController
113+
def create
114+
user = User.create(user_params)
115+
user.avatar = { data: params[:avatar] } # params[:avatar] => 'data:image/png;base64,[base64 data]'
99116
user.save
100117
end
101118

@@ -115,7 +132,7 @@ Check the following example:
115132
class UsersController < ApplicationController
116133
def create
117134
user = User.create(user_params)
118-
user.avatar.attach(data: params[:avatar], filename: 'your_filename', content_type: 'content/type', identify: 'false')
135+
user.avatar.attach(data: params[:avatar], filename: 'your_filename', content_type: 'content/type', identify: 'false') # params[:avatar] => 'data:image/png;base64,[base64 data]'
119136
end
120137

121138
private

lib/active_storage_support/base64_attach.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ module Base64Attach
44
module_function
55

66
def attachment_from_data(attachment)
7+
attachment = attachment.to_h if attachment.is_a?(ActionController::Parameters)
8+
79
if attachment.is_a?(Hash)
810
attachment = attachment.symbolize_keys
911
fill_attachment_data(attachment, attachment.delete(:data))

0 commit comments

Comments
 (0)