ruby on rails - Render new form if params are empty -
i have video upload form. works when video chosen computer , submitted. video gets uploaded. if user clicks on submit button without selecting video, throws following error:
actioncontroller::parametermissing in videoscontroller#create param missing or value empty: video
what doing wrong here?
this video controller:
class videoscontroller < applicationcontroller before_action :find_userinfo before_action :find_video, only: [:destroy] before_action :authenticate_user!, only: [:new] def index end def new @video = video.new end def create @video = video.new(video_params) @video.userinfo_id = @userinformation.id @video.user_id = current_user.id if @video.save redirect_to userinfo_path(@userinformation) else render 'new' end end def edit end def update end def destroy @video.destroy redirect_to userinfo_path(@userinformation) end private def video_params params.require(:video).permit(:introvideo) end def find_userinfo @userinformation = userinfo.friendly.find(params[:userinfo_id]) end def find_video @video = video.find(params[:id]) end end
my form view:
<%= simple_form_for([@userinformation, @userinformation.videos.build]) |f| %> <%= f.file_field :introvideo%> <%= f.button :submit, 'upload video'%> <% end %>
i tried adding following code controller still didn't work:
def create @video = video.new(video_params) @video.userinfo_id = @userinformation.id @video.user_id = current_user.id if @video.video_params.empty? render 'new' end if @video.save redirect_to userinfo_path(@userinformation) else render 'new' end end
any appreciated. thanks.
to check if video
empty this:
def create return render 'new' if params[:video].blank? @video = video.new(video_params) @video.userinfo_id = @userinformation.id @video.user_id = current_user.id if @video.save redirect_to userinfo_path(@userinformation) else render 'new' end end
Comments
Post a Comment