外键只是传递给新表单而不是创建操作(ruby on rails)

the foreign key is just passing to the new form not the create action (ruby on rails)

This is my controllers and routes I have a albums controller and a bands controler with their models, and I want to access the foreign key to pass it, but it told me bands is blank

    def show
        @album = Album.find_by(:id => params[:id])
        render :show
    end
    def new
        @band = Band.find_by(:id => params[:band_id])
        @albums = Album.new(:band_id => params[:band_id])
        render :new
    end
    def create
        @albums = Album.new(albums_params)

        if @albums.save
            flash[:success] = "Album created successfully"
            redirect_to album_path(@albums.id)
        else
            @band = @albums.band
            flash[:error] = @albums.errors.full_messages
            render :new
        end
    end
    def update
        render :edit
    end
    def edit
    end
    def destroy
    end

    private

    def albums_params
        params.require(:albums).permit(:name, :band_id, :live, :year)
    end
end```

  resources :bands do
    resources :albums, :only => :new
  end

尝试像下面这样传递 Band 关系。

def new
  @band = Band.find_by(:id => params[:band_id])
  @albums = Album.new(:band => @band)
  render :new
end

或检查您的代码。你能找到 ID 正确的 Band 吗?

@band = Band.find_by(:id => params[:band_id])

并检查您的观点 你必须像下面这样

<%=form.hidden_field :band_id, value: @albums.band_id%>

<%=form.hidden_field :band_id, value: @band.id %>