【发布时间】:2023-08-13 16:58:01
【问题描述】:
我要做的是将我的 Song 实例的“album_id”属性设置为等于当前正在查看的专辑的 ID。
例如:
http://localhost:3000/artist_profiles/1/albums/2
在上面的 URL 中,我想将我正在创作的歌曲的“album_id”设置为 2。
我的歌曲控制器:
class Albums::SongsController < ApplicationController
before_filter :set_user_friendships
def new
end
def create
# Make an object in your bucket for your song
obj = S3_BUCKET.objects[params[:file].original_filename]
# Upload the file
obj.write(
file: params[:file],
acl: :public_read
)
# Create an object for the song
@song = Song.new(
url: obj.public_url,
name: obj.key
)
@song.album_id = Album.find(params[:id])
# Save the upload
if @song.save
redirect_to artist_profile_albums_path(current_user.id), success: 'File successfully uploaded'
else
flash.now[:notice] = 'There was an error'
render :new
end
end
def index
@user_friendships = current_user.user_friendships.all
@songs = Song.all
end
def song_params
params.require(:song).permit(:id, :url, :name, :song_title, :album_id)
end
def set_user_friendships
@user_friendships = current_user.user_friendships.all #this is here because of partial UGHHH
end
end
更具体地说,以下行:
@song.album_id = Album.find(params[:id])
但是,当我尝试此代码时,我收到以下错误:
Couldn't find Album with 'id'=
如果我尝试:
@song.album_id = Album.find(2).id
或:
@song.album_id = 2
我得到 0 个错误。
运行 rake routes 命令后我正在查看的路线是:
/artist_profiles/:artist_profile_id/albums/:id(.:format)
我将如何获取正确专辑的 ID?
感谢任何帮助!
更新:
我尝试上传歌曲时的日志:
Started POST "/songs" for ::1 at 2016-07-04 07:34:32 -0400
Processing by Albums::SongsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"/pZp493WqiDgkJC72ueK5Wr4eVqdVvWzY43c/ru/OpSt3J9gECwC7nIhszLgyef218sRWsAr9xSZOnH751MUoA==", "file"=>#<ActionDispatch::Http::UploadedFile:0x007ff871c261b0 @tempfile=#<Tempfile:/var/folders/xb/38dybzwn51g3fg4vb7kdgg5m0000gn/T/RackMultipart20160704-11159-5aarh7.mp3>, @original_filename="03 Exit Wounds (Original Mix).mp3", @content_type="audio/mp3", @headers="Content-Disposition: form-data; name=\"file\"; filename=\"03 Exit Wounds (Original Mix).mp3\"\r\nContent-Type: audio/mp3\r\n">, "commit"=>"Upload song"}
User Load (0.5ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT 1 [["id", 1]]
[AWS S3 200 16.701785 0 retries] put_object(:acl=>:public_read,:bucket_name=>"atmosphere-development",:content_length=>13561000,:data=>#<ActionDispatch::Http::UploadedFile:0x007ff871c261b0 @tempfile=#<Tempfile:/var/folders/xb/38dybzwn51g3fg4vb7kdgg5m0000gn/T/RackMultipart20160704-11159-5aarh7.mp3>, @original_filename="03 Exit Wounds (Original Mix).mp3", @content_type="audio/mp3", @headers="Content-Disposition: form-data; name=\"file\"; filename=\"03 Exit Wounds (Original Mix).mp3\"\r\nContent-Type: audio/mp3\r\n">,:key=>"03 Exit Wounds (Original Mix).mp3")
Album Load (0.1ms) SELECT "albums".* FROM "albums" WHERE "albums"."id" = ? LIMIT 1 [["id", nil]]
Completed 404 Not Found in 16748ms (ActiveRecord: 0.9ms)
ActiveRecord::RecordNotFound (Couldn't find Album with 'id'=):
app/controllers/albums/songs_controller.rb:24:in `create'
Rendered /Users/sethjones/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.3/lib/action_dispatch/middleware/templates/rescues/_source.erb (5.1ms)
Rendered /Users/sethjones/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.3/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (2.0ms)
Rendered /Users/sethjones/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.3/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (3.0ms)
Rendered /Users/sethjones/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.3/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout (60.4ms)
Cannot render console with content type multipart/form-dataAllowed content types: [#<Mime::Type:0x007ff8731e25a8 @synonyms=["application/xhtml+xml"], @symbol=:html, @string="text/html">, #<Mime::Type:0x007ff8731e22d8 @synonyms=[], @symbol=:text, @string="text/plain">, #<Mime::Type:0x007ff8731dae20 @synonyms=[], @symbol=:url_encoded_form, @string="application/x-www-form-urlencoded">]
【问题讨论】:
-
可以分享路线和日志吗?
-
其实需要在params[:id]中查看你得到的是哪个id,是专辑id还是歌曲id?
-
我该如何检查呢? Rails 告诉我 ID 不存在。
-
localhost:3000/artist_profiles/1/albums/2 当您在此页面上时,您可以在日志中看到参数,然后出现错误,对吗?所以可以粘贴到这里,这样可以从中追踪到一些东西
-
我认为您没有在下一行中传递专辑 ID,例如。艺术家个人资料专辑路径(current_user.id, :album_id => 2)
标签: ruby-on-rails routes nested params nested-routes