gpt4 book ai didi

ruby-on-rails - 将Youtube剪辑嵌入Rails应用程序

转载 作者:行者123 更新时间:2023-12-03 06:24:22 25 4
gpt4 key购买 nike

我的rails应用程序中有一个管夹。个人资料包括视频介绍。

我有一个带有youtube_url属性的配置文件模型。

我有一个profile_helper,内容如下:

module ProfilesHelper

def embed(youtube_url)
youtube_id = youtube_url.split("=").last
content_tag(:iframe, nil, src: "//www.youtube.com/embed/#{youtube_id}")
end
end


<div class="embed-container">
<%= embed(profile.youtube_url) %>
</div>

但是,当我尝试测试此错误时发现上面的嵌入行有问题,但出现错误:
undefined local variable or method `profile' for #<#<Class:0x00000103f434f0>:0x00000107f84f00>

我的个人资料 Controller 具有:
class ProfilesController < ApplicationController
#decorates_assigned :profile
before_action :authenticate_user!
before_action :set_profile, only: [:show, :edit, :update, :destroy]

# GET /profiles
# GET /profiles.json
def index
@profiles = Profile.all
end

# GET /profiles/1
# GET /profiles/1.json
def show


end

# GET /profiles/new
def new
@profile = Profile.new
end

# GET /profiles/1/edit
def edit
end

# POST /profiles
# POST /profiles.json
def create

@profile = Profile.new(profile_params)
@profile.user = current_user
respond_to do |format|
if @profile.save
format.html { redirect_to @profile, notice: 'Profile was successfully created.' }
format.json { render action: 'show', status: :created, location: @profile }
else
format.html { render action: 'new' }
format.json { render json: @profile.errors, status: :unprocessable_entity }
end
end
end

# PATCH/PUT /profiles/1
# PATCH/PUT /profiles/1.json
def update
respond_to do |format|
if @profile.update(profile_params)
format.html { redirect_to @profile, notice: 'Profile was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: @profile.errors, status: :unprocessable_entity }
end
end
end

# DELETE /profiles/1
# DELETE /profiles/1.json
def destroy
@profile.destroy
respond_to do |format|
format.html { redirect_to profiles_url }
format.json { head :no_content }
end
end

private
# Use callbacks to share common setup or constraints between actions.
def set_profile
@profile = Profile.find(params[:id])
end

# Never trust parameters from the scary internet, only allow the white list through.
def profile_params
params[:profile].permit(
:title, :languages, :overview, :research_interests, :occupation, :about_me,
:interested_in, :aspirations, :advice, :average_day, :like_to, :fyi_fact, :timezone, :image, :university_id,
:link_to_external_profile, :youtube_url
)
end
end

关于我做错了什么的任何想法?

谢谢

最佳答案

更改 View 代码,使其引用@profile而不是profile,如下所示:

<div class="embed-container"> 
<%= embed(@profile.youtube_url) %>
</div>

已在 @profile概要文件方法中正确设置了 set_profile实例变量。

关于ruby-on-rails - 将Youtube剪辑嵌入Rails应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29044856/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com